jQuery event object
Now we'll get started with events in jQuery.
When an event is triggered, jQuery generates an event object event, which contains all the information about it. This object is passed to the handler function.
For example, let's find out which tag we click on. Let's take the following HTML code:
<body>
<div id="text"></div>
<div>
<p>
<strong><span>click</span></strong>
</p>
</div>
</body>
CSS looks like this:
span, strong, p {
display: block;
padding: 10px;
border: 1px solid black;
}
Now we will use the event.target.nodeName property of our event object, which in turn we pass to the click method handler function:
$('body').click(function(event) {
$('#text').html('clicked: ' + event.target.nodeName);
});
The event object contains many properties and methods.