jQuery Event How to - Bind a handler to an event (like click) for all current and future matched element








Question

We would like to know how to bind a handler to an event (like click) for all current and future matched element.

Answer


<!--  w w w. j  a  v  a 2 s .c  om-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
              $("div").live("click", function(){
                 $(this).after("<p>Another paragraph!</p>");
              });

        });
    </script>
  </head>
  <body>
    <body>
       <div>div<h1>header 1</h1></div>
    </body>
</html>

The code above is rendered as follows: