I’m using knockout in the following manner successfully:
<div id="Options" data-bind="foreach: Options">
<button type="button" data-bind="css: { selected: IsSelected }, enable: $parent.allow, click: $parent.select"><img src="/path/to/img.png"/></button>
</div>
Now, I’m trying to change the buttons into images with the following:
<div id="Options" data-bind="foreach: Options">
<input type="image" data-bind="css: { selected: IsSelected }, attr:{src:/path/to/img.png}, enable: $parent.allow, click: $parent.select" />
</div>
My goal is to pair every Option to its own image using knockout.
The buttons aren’t loading with the second option, so I’m thinking the syntax is off. Any idea why it’s not working?
The way you’re setting the image source is wrong. You’re trying to set it to the value of a string but you need to write it in as a string literal (i.e., with quotes). The reason you’re able to set the bindings without them in the other bindings is because they are properties of your view model.
Either of these should work: