jQuery Event How to - Check element id for which fires the event








Question

We would like to know how to check element id for which fires the event.

Answer


<!-- w w w . ja  va  2s  .c  om-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.1.js'></script>
<script type='text/javascript'>
$(window).load(function(){
$(document).click(function(e)
   {
       if($(e.srcElement).attr('id')=='id')
       {
           console.log('click on #id');
       }
       else
       {
            console.log('click on something else'); 
       }
   });
});
</script>
</head>
<body>
  <input id="id" value="click here" type="button">
  <p>or click somewhere else</p>
</body>
</html>

The code above is rendered as follows: