knockout.js

Href binding

Remarks#

There is no href binding in the core KnockoutJS library, which is the reason all examples showcase other features of the library to get the same effect.

See also this Stack Overflow question on the same topic.

Using attr binding

<a data-bind="attr: { href: myUrl }">link with dynamic href</a>
ko.applyBindings({
  myUrl: ko.observable("https://www.stackoverflow.com")
});

Since there is no native href binding in KnockoutJS, you need to use a different feature to get dynamic links. The above example showcases the built-in attr binding to get a dynamic link.

Custom binding handler

href binding is not native to KnockoutJS, so to get dynamic links use a custom binding handler:

<a data-bind="href: myUrl">link with dynamic href</a>
ko.bindingHandlers['href'] = {
  update: function(element, valueAccessor) {
    element.href = ko.utils.unwrapObservable(valueAccessor());
  }
};

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow