tags:

views:

563

answers:

4

Hi,

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.

Thanks,
Tee

+3  A: 

Have you looked into onkeyup?

http://www.w3schools.com/jsref/event_onkeyup.asp

gurun8
That'll get you part of the way. The other thing to consider is cut and paste from the Edit menu or context menu.
Tim Down
That's a great point! *thinking*
gurun8
A: 

Make it easy on yourself and use a jQuery plugin like jquery.maxlength. Tutorial at http://www.anon-design.se/demo/maxlength-with-jquery.

Ted
*Not enough jQuery!* ("jquery" word occurs only 3 times in answer to original post.)
npup
+1  A: 

You will need to use onkeyup and onchange 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.

Josh Stodola
A: 

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.

self.setInterval('checkTextAreaValue()', 50);
Ico