jQuery Method How to - Use bind() method to bind click event








Question

We would like to know how to use bind() method to bind click event.

Answer


<!DOCTYPE html>
<html>
<head>
<script class="jsbin"
  src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<style>
article, aside, figure, footer, header, hgroup, menu, nav, section {
  display: block;
}<!--from ww w . j a v  a  2  s  . c o  m-->
</style>
</head>
<body>
  <p id="hello">Hello World</p>
  <hr />
  <div class="log"></div>
  <script>
$(document).ready( function() {
  $("#hello").bind("click", function() {
    $(".log").append("click 1<br/>");
  });
  $("#hello").bind("click", function() {
   $(".log").append("click 2<br/>");
  });
});
</script>
</body>
</html>

The code above is rendered as follows: