Event object

The event object

A jQuery event handler standardizes certain attributes:

.target
represents the DOM element that initiated the event.
.relatedTarget
represents the other DOM element involved in the event, if any. For mouseout, it indicates the element being entered, and for mousein, it indicates the element being exited.
.which
indicates the specific key or button that was pressed.
.pageX
the x coordinate of the mouse cursor relative to the left edge of the page.
.pageY
the y coordinate of the mouse cursor relative to the top edge of the page.
.result
the last value returned by an event handler that was triggered by this event, unless the value was undefined.
.timeStamp
returns the number of milliseconds since January 1, 1970 when the event is triggered.
.preventDefault()
If this method is called, the default action of the event will not be triggered.
.stopPropagation()
This method prevents the event from bubbling up the DOM tree looking for more event handlers to trigger.

Returning false from a handler is equivalent to calling both .preventDefault() and .stopPropagation() on the event object.

Using the event object in a handler looks like this:


$(document).ready(function() {
 $('#foo').bind('click', function(event) {
    alert('The mouse cursor is at ('+ event.pageX + ', ' + event.pageY + ')');
 });
});

Passing event data

The optional eventData parameter allows us to pass additional information to the handler.


var message = 'hi!';
$('#foo').bind('click', {msg: message}, function(event) {
 alert(event.data.msg);
});
message = 'test!';
$('#bar').bind('click', {msg: message}, function(event) {
 alert(event.data.msg);
});

add some extra data before the event handler


function handler(event) {
  alert(event.data.foo);
}
$("p").bind("click", {foo: "bar"}, handler)
Home 
  JavaScript Book 
    jQuery  

Event:
  1. jQuery Event
  2. jQuery's methods for event
  3. Event object
  4. event.keycode
  5. event.clientX/clientY
  6. event.pageX/pageY: click event coordinates
  7. event.preventDefault()
  8. event.stopPropagation(): Stop only an event from bubbling by using the stopPropagation method.
  9. event.target.tagName
  10. event.which:check key code
  11. return false to Cancel a default action and prevent it from bubbling up
  12. bind
  13. .blur()
  14. .change()
  15. .click()
  16. .error()
  17. .dblclick()
  18. .delegate()
  19. die:Removes a bound live event
  20. .focus()
  21. .hover()
  22. keydown() event
  23. .keypress()
  24. keyup event and check the key code
  25. .live()
  26. .load()
  27. mousedown() event
  28. mouseenter() event
  29. mouseleave
  30. mousemove()
  31. mouseover() event
  32. mouseout
  33. mouseup() event
  34. .off() removes events
  35. .on() replaces the functionality of all the event methods.
  36. .one() method executes handler only once.
  37. .ready()
  38. .resize()
  39. .scroll()
  40. .select()
  41. .submit()
  42. .toggle()
  43. .trigger()
  44. .triggerHandler()
  45. .unbind() accepts a string describing the event type to unbind.
  46. .undelegate() removes the binding
  47. .unload()
  48. use bind/trigger to create custom event