Event object stores information about the event happening on a page.
You can get a hold of it in JQuery by using the handle argument that contain it:
$(document).click(function(evt) {
var xPos = evt.pageX;
var yPos = evt.pageY;
alert(‘X: ” + xPos + ‘ Y: ‘ + yPos);
});
In this case, evt holds the event object. This is an arbitrary name, it can be set by the developer to anything.
Some common event properties:
- pageX – Distance in pixels of the mouse pointer from the left edge of the browser window
- pageY – Distance in pixels of the mouse pointer from the right edge of the browser window
- screenX – Distance in pixels of the mouse pointer from the left edge of the monitor
- screenY – Distance in pixels of the mouse pointer from the right edge of the monitor
- shiftKey – True if the shift key down when the event occurs
- which – Use with the keypress event to determine the numeric code of the key pressed
- target – The target object of the event (example: in click() event, what event was clicked)
- data – JQuery propietary object used with the bind() function to pass data to an event handling function
To prevent the current event default behavior from happening, you can use the handle and call the preventDefault() method, that is native of the Event Object, or just return false as well:
evt.preventDefault(); // don’t follow the link
To remove events using JQuery, you can do the following (example):
$('.tabButton').unbind('click'); // For elements with .tabButton class, they won't respond to the click event
When an event is bind to an element, the children of that element also inherit the event binding.
For instance, if you attach an event to a <div> tag, the elements inside will also have it.
To stop this behavior, you can use the function evt.stopPropagation()