tags:

views:

39

answers:

3

I have a textarea element what i want to limit to, lets say, 100 chars. my current task is to give a visual feedback to the user. Starting with empty box it should be "100 chars left".

Now, i added a javascript function charsLeft() to the textarea's onChange but it works only 50% as intended. It only updates the div containing the number when i leave (focus) the textarea and not as i type. how do i go about to do that? So that my user can see, as they type, how many chars they have left in my textarea?

+1  A: 

Try using the onKeyUp event.

m_oLogin
PERFECTO! :) thanks
Jason94
That won't cover cut and paste, or text dragged into the textarea.
Tim Down
true, but it does fully cover the author's problem ("So that my user can see, as they type"), don't you think? Besides, I'm always reluctant to implementing the paste solution because of its drawbacks towards browser compatibilities (http://www.quirksmode.org/dom/events/cutcopypaste.html)
m_oLogin
I think the OP **should** be concerned about cut/paste/dragging of content, even if he doesn't explictly mention it. the `cut` and `paste` events are implemented in the majority of browsers currently in use so handling those events where they exist has got to be an improvement.
Tim Down
Then I guess it depends where the application is used... For most applications, no support for Firefox / Opera is a no-go (even if it only represents a third of what's used today)
m_oLogin
`cut` and `paste` work fine in Firefox 3.0, so it's nothing like a third. The bug PPK mentions does not constitute a problem for this question. I also suggested a polling solution for other browsers in my answer.
Tim Down
A: 

Handling the keyup event should certainly be part of the solution. You should also consider the cut and paste events, and since text can also be dragged into a textarea you should try and handle that too, possibly using the mouseup event. You may also want some kind of polling to cover browsers such as Firefox 2 and Opera that don't support the cut and paste events.

Tim Down
A: 

If you are looking for a more elegant solution, Flapjax is your friend. See the relevant code sample.

thSoft