I have a simple select multiple with options binding like below :
<select multiple="multiple" width="50" data-bind="options: propositions, optionsText: function(item){ return item.name }, optionsValue: function(item) { return item.name }"></select>
this is able to set item.name on the value attribute of the option item like below :
<option value=" item.name value here ... "> item.name also here </option>
What I would like is to set a custom attribute like this :
<option value=" item.name " data-value = " item.name "> item.name also here </option>
Is it possible to tell knockout to set the attribute data-value in the binding and how would you do that ?
Intead of using options
binding you can use foreach
. In that case you can assign any attributes to the option element:
<select multiple="multiple" width="50" data-bind="foreach: propositions">
<option data-bind="value: name, attr: {'data-value': name}"></option>
</select>
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