tags:

views:

139

answers:

4

I've got a textarea, and when it changes, I'd like a function to be called.

However, the standard onchange event fires after the focus leaves the textarea. What I really want is for the even to fire as soon as the user begins typing.

I could bind onkeypress, but that doesn't handle, say, pasting text into the textarea with the mouse. Plus it fires for Tab and other non-changing keypresses.

Is there a better solution?

+3  A: 

You could add a

onkeyup="SomeFunction();"

Edit: Aah, too quick. I did not see the "on mouse paste".

Edit 2:

<script type="text/javascript">
    var ival = "";
    var checkup = window.setInterval("checkChange();", 100);
    function checkChange() {
        var nval = document.getElementById("test").value;
        if (nval!=ival) { alert("change in the text"); ival=nval; }
     }
</script>
<input type="text" id="test" name="test" value="" />

Example from: http://bytes.com/topic/javascript/answers/703623-capturing-event-when-user-right-click-paste-into-text-box

meep
which does not cover middle click (paste on linux), nor right click->paste.
elcuco
A: 

I know this is ugly... but what about polling the state with a timer...?

elcuco
A: 

Do you want to fire an event for every since letter that is typed in or just the first letter?

Meaning, in this post right now - I probably have over 100 charcters while typing in the text field. Do you want the event to be fired over 100x?

This should be a comment :)
Andy Mikula
A: 

onKeyPress will work, just in the case of pasted coded - you'll know it pasted because it will contain a newline character where as if you were just typing into a textfield - the newline character wouldn't be present

Just check for a newline and use onKeyPress