I'm trying to bind an Input Range Object with Knockout.js. The value binding seems to works well but I can't find a way to update the observable while dragging the slider. The observable is updated only when I release the mouse, giving a bad experience since I'm creating a volume slider.
I've tried every valueUpdate option without any result. They seems made just for text input.
var ViewModel = function() {
this.rangeValue = ko.observable(50);
};
ko.applyBindings(new ViewModel()); // This makes Knockout get to work
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input type="text" data-bind="value: rangeValue"/>
<input type="range" data-bind="value: rangeValue, valueUpdate: 'change'"/>
As stated in this question, you need to listen to the oninput event to get the new value while dragging.
A fast Google search resulted in Knockout.js supporting valueUpdate: 'input' even if not documented.
var ViewModel = function() {
this.rangeValue = ko.observable(50);
};
ko.applyBindings(new ViewModel()); // This makes Knockout get to work
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input type="text" data-bind="value: rangeValue"/>
<input type="range" data-bind="value: rangeValue, valueUpdate: 'input'"/>
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