I'm trying to data-bind an attribute with KnockoutJS to a function, so I can do some string manipulation, and struggling to get it to work.
The following works perfectly fine:
<div data-bind="html: doSomething($data)></div>
However, the following does not:
<a data-bind="attr: { src: doSomething($data)}">link</a>
Could somebody please help me get this working?
There are possibly a couple of problems that you might be facing.
First, you should use href instead of src for an anchor's link.
Second, where does doSomething live? Is it a global function? Does it live on each item in an array or on the top-level view model. If it lives on the root view model, then you can refer to it using $root.doSomething from within the scope of an item that you are doing a foreach on.
Here is a sample: http://jsfiddle.net/rniemeyer/WbxPm/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With