We have the ability to create dynamic web pages by using events. Events are actions that can be detected by your Web Application.
Following are the examples events:
When these events are triggered you can then use a custom function to do pretty much whatever you want with the event. These custom functions call Event Handlers.
Using the jQuery Event Model, we can establish event handlers on DOM elements with the bind() method as follows:
$('div').bind('click', function( event ){ alert('Nobody Home'); });
This code will cause the division element to respond to the click event; when a user clicks inside this division thereafter, the alert will be shown.
Full syntax of the bind() command is :
selector.bind( eventType[, eventData], handler)
Following is the description of the parameters:
eventType: A string containing a JavaScript event type, such as click or submit. Refer to the next section for a complete list of event types.
eventData: This is optional parameter is a map of data that will be passed to the event handler.
handler: A function to execute each time the event is triggered.
Typically, once an event handler is established, it remains in effect for the remainder of the life of the page. There may be a need when you would like to remove event handler.
jQuery provides the unbind() command to remove an exiting event handler. The syntax of unbind() is :
selector.unbind(eventType, handler) or selector.unbind(eventType)
handler: If provided, identifies the specific listener that.s to be removed.
The Below are cross platform and recommended event types which you can bind using JQuery:
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 unneccessary 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 certail attributes which you would need to be accessed.
The Below event properties/attributes are available and safe to access in a platform independent manner:
There is a list of methods which can be called on an Event Object:
Following table lists down important event-related 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.
Following is an example which would triggers the blur event on all paragraphs:
$("p").blur();
Following is an example which would bind a click event on all the <div>:
$("div").click( function () { // do something here });
Here is a complete list of all the support methods provided by jQuery:
Your Query was successfully sent!