html - How can I use Javascript to populate a div with input from an input text box? -


i'm trying run script on input text box populates div. snippet so:

<input type="text" id="control" value="" /> <div id="displaydiv"></div> <script> document.getelementbyid("control").onkeydown = function() { document.getelementbyid("displaydiv").innerhtml = this.value;    } </script> 

the problem here there lag of 1 character, i.e. if type 'a', nothing shows up. when type 'b', previous 'a' shows , on. tried replacing onkeydown onkeyup, , there still bit of lag. how div match input text box in real time, without refreshing page and/or losing focus on input box?

thanks looking.

that because onkeydown triggers before key has added value input. use onkeyup instead. work way after.

edit: not sure if missed speed thing, if edited question, lag seeing unavoidable. close "real-time" going get. it's not bad, less half second when check in a fiddle

edit2: satisfy curiosity, checked performance against knockoutjs's databinding, 1 input updating div text via knockout, , div via script. performance identical. bit surprised knockout didn't introduce noticable overhead. here the fiddle


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -