How do I detect change event on textarea using javascript?
I'm trying to detect how many characters left is available as you type.
I tried using the onchange event, but that seems to only kick in when focus is out.
How do I detect change event on textarea using javascript?
I'm trying to detect how many characters left is available as you type.
I tried using the onchange event, but that seems to only kick in when focus is out.
You will need to use
onkeyup
andonchange
for this. The onchange will prevent context-menu pasting, and the onkeyup will fire for every keystroke.See my answer on How to impose maxlength on textArea for a code sample.
Try this one. It's simple, and since it's 2016 I am sure it will work on most browsers.
You can listen to event on change of textarea and do the changes as per you want. Here is one example.
I know this question was specific to JavaScript, however, there seems to be no good, clean way to ALWAYS detect when a textarea changes in all current browsers. I've learned jquery has taken care of it for us. It even handles contextual menu changes to text areas. The same syntax is used regardless of input type.
or
The best thing that you can do is to set a function to be called on a given amount of time and this function to check the contents of your textarea.
Not tested on Firefox.