Tuesday, 13 August 2013

JQuery-Selector $(this).next("input[name='foo']") doesn't work

JQuery-Selector $(this).next("input[name='foo']") doesn't work

I want to react with .change() on a value change. I that case, an other
(hidden) field must be updated.
<input type="text" name="n1"><br>
<input type="text" name="n2"><br>
<input type="text" name="n3"><br>
<input type="hidden" name="h1"><br>
<script>
$("input[type='text']").change( function() {
try {
$temp = $(this).next("input[type='hidden']");
} catch (e) { // just for testing
alert("not found "+e);
}
try {
$temp = $(this).next("input[name='h1']");
} catch (e) { { // just for testing
alert("not found "+e);
}
$temp.val("hidden");
alert("Temp is a "+$temp);
alert("Temp has following value:"+$temp.val());
})
</script>
Demo for testing at: http://jsfiddle.net/22C2n/1209/
This statement
$temp = $(this).next("input[type='hidden']");
results in a "not defined", or better: $temp is an $object[] and not (as
aspected) $object[input name='h1']
Where is the problem?

No comments:

Post a Comment