Thursday, January 12, 2012

live() in Jquery

Once,the dom tree is created and loaded,If u are adding new elements dynamically,u may need to use live() to access those newly created elements.

Eg;  When a page is loaded,You got a select box with list of countries,
You selected a country and clicked on search button,
you got the list of all the state in that country,along with all the districts(in select box), for each starte in a row

When this page was loaded,list of all the country was loaded into select box,In the Dom it is added.
But when you selected country and clicked on search button,another table was loaded containing all the states,
This table is dynamic and it may not be loaded properly in DOM

Support in that table we have 5 rows,representing 5 state of India,each row is having 1 more select box,containing all the list of district in that state,Now you want to alert A msg on selecting any district

Here you need live()
Code Snippet 
-------------------
<script language="JavaScript">
$(document).ready(f1);
function f1()
{
$("#selDistrict").live("change", function() 
                             {
                                alert("I was clicked");
                              }
 }
</script>










  


   




Means this function() will be called when ever value of selDistrict  (id of select component) is changed

No comments:

Post a Comment