Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between keypress and change in JavaScript

I am newbie to java script. I am bit confused about key-press and change event.

Following is sample code

 $("input").live('change',function ()
    {
       Alert("Change event call");
    });

    /
    $("input").live('keypress',function ()
    {            
       Alert("Keypress event call");
    });

When exactly Keypress event is call and change event call?

like image 398
Raje Avatar asked Feb 01 '26 08:02

Raje


1 Answers

Change event

The change event is sent to an element when its value changes. This event is limited to <input> elements, <textarea> boxes and <select> elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

Keypress event

The keypress event is sent to an element when the browser registers keyboard input. This is similar to the keydown event, except in the case of key repeats. If the user presses and holds a key, a keydown event is triggered once, but separate keypress events are triggered for each inserted character. In addition, modifier keys (such as Shift) trigger keydown events but not keypress events.

A keypress event handler can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.

References:

  • http://api.jquery.com/change/
  • http://api.jquery.com/keypress/
like image 127
VisioN Avatar answered Feb 03 '26 21:02

VisioN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!