I want to have it so when I type into a textbox (or an input field) and have whatever I am typing simultaneously appear in a div or just other place on the page?
The stackoverflow site does this when we type up a question. As we type a question, it shows whatever we are typing in a box below the textbox.
Hope my question makes sense!
Thanks in advance.
The StackOverflow site does a bit more because it does syntax highlighting too, but the basic idea is to listen to the keyboard events –
keydown(better),keypressorkeyup(not so good if a button is continuously pressed) and in its callback update the value of the target container with the value of the source container. Here’s a quick example.The best solution is to bind to both –
keyupand (keydownorkeypress). Reason being when thekey(press|down)callbacks are fired, the value of the textbox is still the old value so when when we update the target container, it doesn’t get the last character.keyupon the other hand is fired after the value of the text box is updated. However,keyupwill not fire if you keep a button pressed as the button is only released once, so the target updates at the very end. The solution is to use both 🙂See an example here.