Bobby Thermin Bobby Thermin - 6 months ago 8
HTML Question

Calling JS Function from Different File in HTML

I am trying to have an html slider call a javascript function from another file. I have this for my html:

<input type="range" min="0" max="20*Math.PI" value="10" id="angularVelCarousel" onChange="changeValCarousel(this.value)"/>
<span id="valueCarousel">10</span>
<script src='slider.js'></script>


and this for my js (in a file called slider.js)

function changeValCarousel(value){

document.getElementById('valueCarousel').innerHTML = value;


}


how can I make it so that when the slider changes, it executes the js file above?
Thanks!

Answer

Maybe something like this:

function changeValCarousel(value){
    document.getElementById('valueCarousel').innerHTML = value;
}

var slider = document.getElementById('angularVelCarousel');
slider.onchange=function(){changeValCarousel(slider.value)};

Full code here https://jsfiddle.net/ta434v5p/

Comments