Getting value from a range input (slider)

MightyMouse picture MightyMouse · Oct 31, 2013 · Viewed 14.9k times · Source

I am adding a slider dynamically on a page using a string like the one below:

"<input type=\"range\" name=\"aName\" min=\"1\" max=\"9\"/>";

Then, after I append it to the page using plain javascript I can delegate using the following code:

$('input[name=aName]').on('change', function () { alert(this.value)});

and indeed when the value changes, I receive the alert with the correct value. However, if I delegate like this:

$('input[name=aName]').on('change', handleChange);

and define the function later on like this:

function handleChange () {
    var theValue = $('input[name=aName]').value;
    alert("value: " + theValue);
}

I receive the alert with the message "value: undefined". Why is this thing happening?

Answer

Venkata Krishna picture Venkata Krishna · Oct 31, 2013

This is wrong $('input[name=aName]').value. I think you're mixing javascript & jquery.

For getting the value, use like this.

$('input[name=aName]').val()