![jQuery Tutorial](/jquery/images/jquery-mini-logo.jpg)
- jQuery Tutorial
- jQuery - Home
- jQuery - Overview
- jQuery - Basics
- jQuery - Syntax
- jQuery - Selectors
- jQuery - Events
- jQuery - Attributes
- jQuery - AJAX
- jQuery DOM Manipulation
- jQuery - DOM
- jQuery - Add Elements
- jQuery - Remove Elements
- jQuery - Replace Elements
- jQuery CSS Manipulation
- jQuery - CSS Classes
- jQuery - Dimensions
- jQuery - CSS Properties
- jQuery Traversing
- jQuery - Traversing
- jQuery - Traversing Ancestors
- jQuery - Traversing Descendants
- jQuery References
- jQuery - Selectors
- jQuery - Events
- jQuery - Effects
- jQuery - HTML/CSS
- jQuery - Traversing
- jQuery - Miscellaneous
- jQuery - Properties
- jQuery - Utilities
- jQuery Plugins
- jQuery - Plugins
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery Useful Resources
- jQuery - Questions and Answers
- jQuery - Quick Guide
- jQuery - Useful Resources
- jQuery - Discussion
jQuery event.target Property
The jQuery event.target property is used to retrieve the DOM element that triggered the event. This can either be the element that is directly registered for the event or one of its children.
Syntax
Following is the syntax of the jQuery event.target property −
event.target
Parameters
This property does not accept any parameter.
Return Value
This property returns the DOM element that triggered the event.
Example 1
The following is the basic example of the jQuery event.target property.
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> </head> <body> <p>Click on the below button</p> <button>Click me</button> <script> $('button').click(function(event){ alert("Triggered by the " + event.target.tagName + " element"); }) </script> </body> </html>
Output
The above program displays a button. When clicked, the tag name of the element will be shown in a pop-up alert −
When button is clicked −
Example 2
Following is another example of the jQuery event.target property. We use this property to retrieve the DOM element that triggered the event.
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> <style> div, p, span{ padding: 8px; display: block; border: 1px solid #999; } </style> </head> <body> <div class="res"></div> <div> <p> <span>Click</span> </p> </div> <script> $('body').on("click", function(event){ $('.res').text("Triggered by the " + event.target.nodeName); }) </script> </body> </html>
Output
After executing the above program, an interface similar to event bubbling is displayed. When users click on any element, the tag name of that element will be shown within another div −
Example 3
In the example below, we use the event.target property to retrieve the multiple DOM elements on which that event was triggered −
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> <style> p, h1, div, button{ font-size: 20px; display: block; padding: 10px 0px; } span{ color: green; padding: 10px; font-size: 20px; } </style> </head> <body> <p>Click on any element</p> <h1>This is heading</h1> <div>This is div element</div> <button>Click me!</button> <span></span> <script> $('p, h1, div, button').click(function(event){ $('span').text("Triggered by the " + event.target.tagName + " element"); }) </script> </body> </html>
Output
When the program is executed, it displays a <p>, <h1>, <div>, and a <button> element. When users click on any of these elements, the event triggered by the tag name will be displayed −