JQuery Programmer Question:
Download Job Interview Questions and Answers PDF
Explain bind() vs live() vs delegate() methods?
Answer:
► The bind() method will not attach events to those elements which are added after DOM is loaded while live() and delegate() methods attach events to the future elements also.
The difference between live() and delegate() methods is live() function will not work in chaining. It will work only on an selector or an element while delegate() method can work in chaining.
For example
$(document).ready(function()
{
$("#myTable").find("tr").live("click",function()
{
alert($(this).text());
});
});
Above code will not work using live() method. But using delegate() method we can accomplish this.
$(document).ready(function()
{
$("#dvContainer")children("table").delegate("tr","click",function()
{
alert($(this).text());
});
});
The difference between live() and delegate() methods is live() function will not work in chaining. It will work only on an selector or an element while delegate() method can work in chaining.
For example
$(document).ready(function()
{
$("#myTable").find("tr").live("click",function()
{
alert($(this).text());
});
});
Above code will not work using live() method. But using delegate() method we can accomplish this.
$(document).ready(function()
{
$("#dvContainer")children("table").delegate("tr","click",function()
{
alert($(this).text());
});
});
Download JQuery Programmer Interview Questions And Answers
PDF
Previous Question | Next Question |
Explain the animate function? | Can we use our own specific character in the place of $ sign in jQuery? |