JQuery Events

events in jquery

JQuery Events are the actions that can be detected by your web application. They are used to create dynamic web pages. An event shows the exact moment when something happens.

Following are the examples events :

  • A mouse click
  • A web page loading
  • An HTML form submission
  • Scrolling of the web page etc.
  • A keystroke on your keyboard, etc.

JQuery Events Type’s

The following are cross platform and recommended event types which you can bind using JQuery :

Sr.No.Event Type        Description
1.blurOccurs when the element loses focus.
2.changeOccurs when the element changes.
3.clickOccurs when a mouse click.
4.dblclickOccurs when a mouse double-click.
5.errorOccurs when there is an error in loading or unloading etc.
6.focusOccurs when the element gets focus.
7.keydownOccurs when key is pressed.
8.keypressOccurs when key is pressed and released.
9.keyupOccurs when key is released.
10.loadOccurs when document is loaded.
11.mousedownOccurs when mouse button is pressed.
12.mouseenterOccurs when mouse enters in an element region.
13.mouseleaveOccurs when mouse leaves an element region.
14.mousemoveOccurs when mouse pointer moves.
15.mouseoutOccurs when mouse pointer moves out of an element.
16.mouseoverOccurs when mouse pointer moves over an element.
17.mouseupOccurs when mouse button is released.
18.resizeOccurs when window is resized.
19.scrollOccurs when window is scrolled.
20.selectOccurs when a text is selected.
21.submitOccurs when form is submitted.
22.unloadOccurs when documents is unloaded.

The Event Object:

The callback function takes a single parameter; when the handler is called the JavaScript event object will be passed through it.

The event object is often unnecessary and the parameter is omitted, as sufficient context is usually available when the handler is bound to know exactly what needs to be done when the handler is triggered, however there are certain attributes which you would need to be accessed.


   The JQuery Events Attributes

Property         Description
altKeySet to true if the Alt key was pressed when the event was triggered, false if not. The Alt key is labeled Option on most Mac keyboards.
ctrlKeySet to true if the Ctrl key was pressed when the event was triggered, false if not.
dataThe value, if any, passed as the second parameter to the bind() command when the handler was established.
keyCodeFor keyup and keydown events, this returns the key that was pressed.
metaKeySet to true if the Meta key was pressed when the event was triggered, false if not. The Meta key is the Ctrl key on PCs and the Command key on Macs.
pageXFor mouse events, specifies the horizontal coordinate of the event relative from the page origin.
pageYFor mouse events, specifies the vertical coordinate of the event relative from the page origin.
relatedTargetFor some mouse events, identifies the element that the cursor left or entered when the event was triggered.
screenXFor mouse events, specifies the horizontal coordinate of the event relative from the screen origin.
screenYFor mouse events, specifies the vertical coordinate of the event relative from the screen origin.
shiftKeySet to true if the Shift key was pressed when the event was triggered, false if not.
targetIdentifies the element for which the event was triggered.
timeStampThe timestamp (in milliseconds) when the event was created.
typeFor all events, specifies the type of event that was triggered (for example, click).
whichFor keyboard events, specifies the numeric code for the key that caused the event, and for mouse events, specifies which button was pressed (1 for left, 2 for middle, 3 for right).

Example:

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = “text/javascript”
         src = “https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js”>
      </script>
      <script type = “text/javascript” language = “javascript”>
         $(document).ready(function() {
            $(‘div’).bind(‘click’, function( event ){
               alert(‘Event type is ‘ + event.type);
               alert(‘pageX : ‘ + event.pageX);
               alert(‘pageY : ‘ + event.pageY);
               alert(‘Target : ‘ + event.target.innerHTML);
            });
         });
      </script>
      <style>
         .div{ margin:10px;padding:12px; border:2px solid #666; width:60px;}
      </style>
   </head>
   <body>
      <p>Click on any square below to see the result:</p>
      <div class = “div” style = “background-color:blue;”>ONE</div>
      <div class = “div” style = “background-color:green;”>TWO</div>
      <div class = “div” style = “background-color:red;”>THREE</div>
   </body>
</html>

The Event Methods:

There is a list of methods which can be called on an Event Object :

  1. preventDefault() : Prevents the browser from executing the default action.
  2. isDefaultPrevented() : Returns whether event.preventDefault() was ever called on this event object.
  3. stopPropagation() : Stops the bubbling of an event to parent elements, preventing any parent handlers from being notified of the event.
  4. isPropagationStopped() : Returns whether event.stopPropagation() was ever called on this event object.
  5. stopImmediatePropagation() : Stops the rest of the handlers from being executed.
  6. isImmediatePropagationStopped() : Returns whether event.stopImmediatePropagation() was ever called on this event object.

JQuery Events Manipulation Methods:

Following table lists down important event-related methods :

i) bind( type, [data], fn ) : Binds a handler to one or more events (like click) for each matched element. Can also bind custom events.

ii) off( events [, selector ] [, handler(eventObject) ]  ) : This does the opposite of live, it removes a bound live event.

iii) hover( over, out ) : Simulates hovering for example moving the mouse on, and off, an object.

iv) on( events [, selector ] [, data ], handler  ) :</b> Binds a handler to an event (like click) for all current and future matched element. Can also bind custom events.

v) one( type, [data], fn ) : Binds a handler to one or more events to be executed once for each matched element.

vi) ready( fn ) : Binds a function to be executed whenever the DOM is ready to be traversed and manipulated.

vii) trigger( event, [data] ) : Trigger an event on every matched element.

viii) triggerHandler( event, [data] ) : Triggers all bound event handlers on an element.

ix) unbind( [type], [fn] ) : This does the opposite of bind, it removes bound events from each of the matched elements.


JQuery Events Helper Methods:

jQuery also provides a set of event helper functions which can be used either to trigger an event to bind any event types mentioned above.

Trigger Methods:

Following is an example which would triggers the blur event on all paragraphs :

Example:

$(“p”).blur();

Binding Methods:

Following is an example which would bind a click event on all the <div> :

Example:

$(“div”).click( function () {
   // do something here
});

Here is a complete list of all the support methods provided by jQuery  :

Sr.No.Method       Description
1.blur( )Triggers the blur event of each matched element.
2.blur( fn )Bind a function to the blur event of each matched element.
3.change( )Triggers the change event of each matched element.
4.change(fn)Binds a function to the change event of each matched element.
5.click( )Triggers the click event of each matched element.
6.click( fn )Binds a function to the click event of each matched element.
7.dblclick( )Triggers the dblclick event of each matched element.
8.dblclick( fn )Binds a function to the dblclick event of each matched element.
9.error( )Triggers the error event of each matched element.
10.error( fn )Binds a function to the error event of each matched element.
11.focus( )Triggers the focus event of each matched element.
12.focus( fn )Binds a function to the focus event of each matched element.
13.keydown( )Triggers the keydown event of each matched element.
14.keydown( fn )Bind a function to the keydown event of each matched element.
15.keypress( )Triggers the keypress event of each matched element.
16.keypress( fn )Binds a function to the keypress event of each matched element.
17.keyup( )Triggers the keyup event of each matched element.
18.keyup( fn )Bind a function to the keyup event of each matched element.
19.load( fn )Binds a function to the load event of each matched element.
20.mousedown( fn )Binds a function to the mousedown event of each matched element.
21.mouseenter( fn )Bind a function to the mouseenter event of each matched element.
22.mouseleave( fn )Bind a function to the mouseleave event of each matched element.
23.mousemove( fn )Bind a function to the mousemove event of each matched element.
24.mouseout( fn )Bind a function to the mouseout event of each matched element.
25.mouseover( fn )Bind a function to the mouseover event of each matched element
26.mouseup( fn)Bind a function to the mouseup event of each matched element.
27.resize( fn)Bind a function to the resize event of each matched element.
28.scroll( fn)Bind a function to the scroll event of each matched element.
29.select( )Trigger the select event of each matched element.
30.select( fn)Bind a function to the select event of each matched element.
31.submit( )Trigger the submit event of each matched element.
32.submit( fn)Bind a function to the submit event of each matched element.
33.unload( fn)Binds a function to the unload event of each matched element.


Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *