Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout set custom attributes with options binding

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 ?

like image 780
Arno 2501 Avatar asked Feb 07 '13 16:02

Arno 2501


1 Answers

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>
like image 184
Artem Vyshniakov Avatar answered Dec 11 '22 23:12

Artem Vyshniakov