The jQuery Event Object is an object that is automatically passed to event handler functions. It contains detailed information about the event such as the event type, target element, mouse position, key pressed, and methods to control event behavior.
The event object is usually accessed using a parameter commonly named event or e.
// Basic syntax of accessing the event object
$("button").click(function(event){
console.log(event.type);
});
// Accessing event properties
$("#box").on("mousemove", function(e){
$("#info").text("X: " + e.pageX + " Y: " + e.pageY);
});
As you move the mouse inside the box, the pageX and pageY properties of the event object show the mouse position relative to the document.
Move your mouse inside the area below to see event data update in real time.
Click the button or type in the input field to see how the event object captures different data.
// Tracking multiple event properties
$("#testBtn, #testInput").on("click keydown", function(e){
$("#eventType").text(e.type);
$("#eventTarget").text(e.target.tagName);
$("#eventWhich").text(e.which || "N/A");
});
e.preventDefault() to stop default actionse.stopPropagation() to stop event bubblinge for readabilitye.target when clicking different elementskeydown event