Answer the question
In order to leave comments, you need to log in
How to bind knockoutjs DOM element attribute?
There is such a select
<select data-bind="value: productPrice">
<option data-price="2">Цена $2.00</option>
<option data-price="4">Цена $4.00</option>
</select>
Ваша цена <span data-bind="text: productPrice"></span>
Answer the question
In order to leave comments, you need to log in
For example. you can write a custom binding for this case
var viewModel = {
productPrice: ko.observable()
};
ko.bindingHandlers.selectedAttribute = {
init: function(element, valueAccessor) {
var attr = valueAccessor().attr,
property = valueAccessor().property;
function updateModel() {
property(element.options[element.selectedIndex].getAttribute(attr));
}
element.addEventListener('change', updateModel);
updateModel();
}
};
ko.applyBindings(viewModel);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question