Live update an input value when sliding a jQuery Ui slider and set the slider value when the input is interacted with.
For this you need jQuery and jQuery UI, add in Bootstrap and a jQuery UI theme to give it a decent appearance.
The slider HTML elements:
<div class="custom-progress progress-up" style="width: 100%">
<div id="rating_slider" class="ui-slider ui-corner-all ui-slider-horizontal ui-widget ui-widget-content">
</div>
</div> The input:
<label for="rating_input">Rating</label> <input type="number" class="form-control" id="rating_input" value="50" min="0" max="100">
Initialize the slider with a minimum value of 0, a maximum value of 100 and a start value of 50. Add two events which call a function on slide and change states
$("#rating_slider").slider({
min: 0, max: 100, value: 50,
slide: function (event, ui) {//When sliding the toggle
updateInputFromSlider("#rating_input", $(this).slider("value"));
},
change: function (event, ui) {//After releasing the toggle
updateInputFromSlider("#rating_input", $(this).slider("value"));
}
}); updateInputFromSlider function will set the input value to that of the slider
function updateInputFromSlider(input_id, value) {
$(input_id).val(value);
} Finally, when interacting with the input manually, update the slider value and position:
$(document).ready(function () {
$("#rating_input").change(function () {
$("#rating_slider").slider("value", $(this).val());
$("#rating_slider").prop("value", $(this).val());
});
});
A drained and empty Kennington reservoir images from a drone in early July 2024. The…
Merrimu Reservoir from drone. Click images to view larger.
Some early September spring shots from a bushwalk, It is still too early for wildflowers.
I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…
If you ever needed a simple solution in Laravel to delete an entire directory and…
How to enforce only one user to register or be registered in a Laravel web…