Firing javascript events when textbox changes

Just a little heads up to anyone putting client code on a textbox.  If you want some  javascript to run when the text in a textbox changes, use OnKeyUp instead of the intuitive OnChange event.  OnChange will only fire after the textbox has lost focus, rather than as the keys are pressed in the textbox, whereas OnKeyUp will fire when, well, when a key comes up!

Of course, if you only want your code to run when the textbox loses focus anyways, then continue using OnChange :)

9 Comments so far »

  1. Anonymous said,

    Wrote on March 2, 2006 @ 1:40 pm

    If I remember correctly, OnKeyUp does not fire if you paste text into the textbox (Right-click, Paste). Just something to keep in mind if you must know every time the text changes.

  2. breichelt said,

    Wrote on March 2, 2006 @ 2:38 pm

    Good call Joshua, to cover that case you would want to put some code into the OnChange event then, or perhaps a mouse event.

  3. gtrfinger said,

    Wrote on October 25, 2006 @ 9:39 pm

    Or you could simply disable the right click context menu from showing up when right clicking on the textbox, by adding this to the textbox control: onContextMenu=’javascript:return false;’

    Cheers

  4. tarek said,

    Wrote on July 10, 2007 @ 1:11 am

    thanxs alot

  5. Marc Smith said,

    Wrote on September 19, 2007 @ 5:24 pm

    A text box on my form is being updated without any user interaction. I’m needing to capture an event to validate the value of the text box. OnChange is only triggered by a user. What else might I use to accomplish this task?

  6. Ramesh said,

    Wrote on February 12, 2008 @ 12:19 am

    After right click and paste in the textbox if the button didn’t get enable, use OnMouseOut event and same functionality as for OnKeyUp. It works fine.

  7. sdsds said,

    Wrote on April 14, 2008 @ 1:55 am

    dsdsdsd

  8. Hardik said,

    Wrote on April 22, 2008 @ 3:25 pm

    ponda hardik girirshbhai

  9. Tarasankar said,

    Wrote on May 26, 2008 @ 2:27 am

    Can any body give any example how to make the button enable on rightClick->Paste event in the textbox..

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Enter my name (ben) in this box, so I know you're a human.

Comment: