jQuery HTML Element How to - Handle click event on anchor tag








Question

We would like to know how to handle click event on anchor tag.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.7.1.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from   w w  w  .  j  av  a 2  s. c  o  m-->
    $('#thumbContainer').on("click","a", function(){ 
      console.log("will work now"); 
    }); 
    var thumb = $('<a class="tinythumb">thumb</a>');
    $("#thumbContainer").append(thumb);
});
</script>
</head>
<body>
  <div id="thumbContainer"></div>
</body>
</html>

The code above is rendered as follows: