I would like a span to update when a value is entered into a text field using jquery. My form field has a text box with the name "userinput" and i have a span with the id "inputval". Any help would be greatly appreciated.
+1
A:
Try this. Be sure that you understand what is going on here.
// when the DOM is loaded:
$(document).ready(function() {
// find the input element with name == 'userinput'
// register an 'keydown' event handler
$("input[name='userinput']").keydown(function() {
// find the element with id == 'inputval'
// fill it with text that matches the input elements value
$('#inputval').text(this.value);
}
}
TM
2009-05-13 03:35:03
+4
A:
UPDATE: although you marked this as the correct answer, note that you should use the keyup event rather than the change event or the keydown
$(document).ready(function() {
$('input[name=userinput]').keyup(function() {
$('#inputval').text($(this).val());
});
});
Marwan Aouida
2009-05-13 03:36:58
I guess great minds think alike! Or maybe it was just really trivial?
TM
2009-05-13 03:38:13
:) it is not a complex problem
Marwan Aouida
2009-05-13 03:39:31
A:
$(function() {
$("input[name=userinput]").keydown(
function() {
$('#inputval').text(this.value);
}
)
})
eKek0
2009-05-13 03:40:24
it is the correct answer, he should use the keydown event rather than the change event.but you should not reselect he input element again as TM said
Marwan Aouida
2009-05-13 03:45:37
This won't actually work. `text()` will always return an empty string since the input has no inner text, it only has a value. Also, there is no need to find the element again, since you already have a reference to it in the event handler (this)
TM
2009-05-13 03:45:51