Thursday, May 24, 2012

Handle event using .live() in jQuery

jQuery .live() can be used to bind a event handler with a not yet existed DOM elements.

<!doctype html>
<head>
 <title>Mobile-Web-App: jQuery .live()</title>
 <meta charset="utf-8">
 <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
 <script>
  $(document).ready(function(){
  
   //"#clickme" not yet existed
   $("#clickme").live('click', (function(event){
    alert("'clickme' Clicked!");
   }));
    
   $("<p id='clickme'>Click Me</p>").appendTo("#addhere");
  });
 </script>
 
</head>
<body>
<p><b>Mobile-Web-App: jQuery .live()</b></P>
<div id="addhere"></div>
</body>
</html>


Handle event using .live() in jQuery


Related:
- jQuery .click() - click event handler
- Handle event using .bind() in jQuery
- Handle event using .bind() and .unbind() in jQuery

Next:
- jQuery .live() and .die()


No comments:

Post a Comment