Href binding

Other topics

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("http://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());
  }
};

Contributors

Topic Id: 6582

Example Ids: 22503,22504

This site is not affiliated with any of the contributors.