jQuery > Events

Find target in jQuery

How to find out which element has initiated a particular event in jQuery?


To know which element has initiated a particular event, event.Target can be used.

  <div id="divEventTarget" style="background:#dedede;width:300px;height:100px;">
  <br />
    <p>event.target demo <br />
        <span>
           this is span 
        </span>
        <br />- This is the p
    </p>
  <br />
</div>

<script>
    $("#divEventTarget").click(function (e) {
        alert(e.target.nodeName);
    });
</script>

In the above code when user clicks the outer boundary of the div whose id is "divEventTarget" then “DIV” will be alerted, when user will click “event.target demo” text then “P” will be alerted and when clicks “this is span” then “SPAN” will be alerted and so on.

All this happens because we have attached a click event on the div element and in the calling function alerted the "e.target.nodeName".

 Views: 6808 | Post Order: 69



Write for us






Hosting Recommendations