I need to live update a text field in html using javascript, that is as the user is typing text in one field, the contents are being displayed in another as they type. i need just a short code snippet to do that. eg as a person is typing their name, the name is being typed one letter at a time at another position on the screen.
1 Answer
Give the two fields an id each, then use something like this:
document.getElementById("field1").onkeyup = function() {
document.getElementById("field2").value = this.value;
}
All it does is bind an event listener to the onkeyup event, which fires every time a key is released. In the event listener, it simply copies the value of the field which received the event to another field, specified by id.
Here's a working example.
1 Comment
James Allardice
@ThinkingStiff - Thanks, not sure how that happened! I've fixed it now.