event.target Property - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:event.target

Description

The event.target property returns which DOM element triggered the event.

Syntax

Parameter Require Description
event Required.event parameter comes from the event binding function

The following code shows how to Return which DOM element triggered the event:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("p, button, h1").click(function(event){
        $("div").html("Triggered by a " + event.target.nodeName + " element.");
    });//from   w w  w.  j  a va  2  s.c  om
});
</script>
</head>
<body>

<h1>This is a heading</h1>

<p>This is a paragraph</p>
<button>This is a button</button>
<p>Click on each element to display which element triggered the event.</p>
<div style="color:blue;"></div>

</body>
</html>

Related Tutorials