Sunday 23 July 2017

Difference Between Live And Bind Function In jQuery ?


These functions allow us to add event handler to any DOM object.
Bind method works only for the present tags in the page, which means the already existing DOM elements, not for the tags which are getting added after this method
or in the future.Now if you click on the <p> tag which we appended at run time, it will just alert the Live function, not the bind function. 
Bind method does not work for the future tag added in the page,  where live does work for that.

<p>This is a p tag</p>  
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>   
<script>
    $("p").bind("click", function () { alert("P tag is clicked from bind function.") });
    $("p").live("click", function () { alert("P tag is clicked from live function.") });
    $("body").append("<p>This is a future p tag.</p>")
</script>
<body>
    <p>Existing P Tag in html page.</p>

</body>

No comments:

Post a Comment