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?
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?
Share Improve this question asked Oct 31, 2013 at 20:52 MightyMouseMightyMouse 13.9k8 gold badges35 silver badges45 bronze badges 2-
what is
handleChange
? there is onlyhandleRQTSliderChange
– Venkata Krishna Commented Oct 31, 2013 at 20:53 - That was a mistake on my side. Clearly it is handleChange in both cases. I have edited the post. – MightyMouse Commented Oct 31, 2013 at 20:54
1 Answer
Reset to default 4This is wrong $('input[name=aName]').value
. I think you're mixing javascript & jquery.
For getting the value, use like this.
$('input[name=aName]').val()