jQuery event.target get element id that fired an event

Description

jQuery event.target get element id that fired an event

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Get the Element that Triggered the Event in jQuery</title>
<style>
    div, p, span{
        padding: 30px;
        display: block;
        border: 3px solid #999;
    }//ww  w.  j a  v a  2s . c  o  m
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
    $(document).click(function(event){
        document.getElementById("demo").innerHTML = 
           "You've clicked: " + event.target.nodeName + ", id: " + event.target.id;
    });
});
</script>
</head>
<body>
    <p id="demo"></p>
    <div id="myDiv">
        <p id="myP">
            <span id="mySpan">Just click anywhere.</span>
        </p>
    </div>
</body>
</html>



PreviousNext

Related