Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout.js bind slider range input

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'"/>
like image 945
Jamby Avatar asked Feb 09 '23 22:02

Jamby


1 Answers

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'"/>
like image 67
Jamby Avatar answered Feb 12 '23 13:02

Jamby