Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass value of an input range to a variable in JS?

How do you get the value of an input range slider into a variable? Below is the range I'm using. Suppose I drag the range to 12, then I need "12" as the variable that I want to then pass to a function.

 <input type="range" min="1" max="30" value="15" />

Edit: I don't want a button to confirm the value or something, I want that everytime the value is changed, it gets passed to the function, so it'll be dynamic!

PS: It may not be the best question out there, but I've honestly tried looking for an answer before posting the question.

like image 447
JohnMichael Avatar asked May 16 '16 16:05

JohnMichael


People also ask

How do you input variables in JavaScript?

In JavaScript, we use the prompt() function to ask the user for input. As a parameter, we input the text we want to display to the user. Once the user presses “ok,” the input value is returned. We typically store user input in a variable so that we can use the information in our program.


Video Answer


1 Answers

You just need to bind to the change event:

<input type="range" min="1" max="30" value="15" />

$("input").change(function(){
    var value = $(this).val();
    alert(value);
})
like image 116
Alvaro Flaño Larrondo Avatar answered Oct 05 '22 13:10

Alvaro Flaño Larrondo