Question

I want to set the id of an input box which am getting in for loop using knockout. How do I set this?

<div data-bind="foreach: testData">
    <span class="cld" data-bind="text:flid"/>
    <input type="hidden" id="hid_freq_{flid}" name="hid_freq_{flid}">
 </div>  

In the above code i am getting flid as data in a span I want to use that same data as an id for an input box, so please let me know whether it is possible or not.

Était-ce utile?

La solution

You need to use the attr binding to set any attributes like id or name

<span class="cld" data-bind="text:flid"/>
<input type="hidden" 
   data-bind="attr: { id: 'hid_freq_' + flid(), name: 'hid_freq_' + flid() }">

Note: you only need to write flid() only if your flid property is a ko.observable otherwise you just need 'hid_freq_' + flid.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top