Skip to content Skip to sidebar Skip to footer

Html5 Slider With Onchange Function

I have a slider (input type range) that is supposed to run a function when the value is being changed. The function should then display the new value in a separate div container. A

Solution 1:

It works, you just need to make sure that the JavaScript function is defined when the element is rendered, for example:

<script>functionupdateSlider(slideAmount) {
        var sliderDiv = document.getElementById("sliderAmount");
        sliderDiv.innerHTML = slideAmount;
    }
</script><inputid="slide"type="range"min="1"max="100"step="1"value="10"onchange="updateSlider(this.value)"><divid="sliderAmount"></div>

See this demo: https://jsfiddle.net/Mmgxg/

A better way would be to remove the inline onchange attribute:

<input id="slide"type="range" min="1" max="100" step="1" value="10">
<div id="sliderAmount"></div>

And then add the listener in your JavaScript code:

var slide = document.getElementById('slide'),
    sliderDiv = document.getElementById("sliderAmount");

slide.onchange = function() {
    sliderDiv.innerHTML = this.value;
}

https://jsfiddle.net/PPBUJ/

Post a Comment for "Html5 Slider With Onchange Function"