InterviewSolution
| 1. |
In jQuery, distinguish between the bind(), live(), and delegate() functions. |
|
Answer» The bind() function does not connect events to items added after loading the DOM. In contrast, the live() and delegate() methods also attach events to future items. The distinction between live() and delegate() methods is that live() does not support chaining. It will only function on a selection or an element. However, the delegate() function supports chaining. The bind() method works only within a dom element. If you want to bind events to items in a different element, you can use the delegate() function. The bind() method is not recommended for use in JavaScript. It is a best practice to call event handlers on items as they are added to the DOM. The bind() method is only used to control the order in which items are displayed. If you need to add items to a different order, you can use the delegate() function. The bind() method is not intended to be used in a production environment. It does not offer the same performance benefits as the delegate() method. For Example - Consider the below code - $(document).ready(function(){$("#interviewbit").find("p").live("click", function(){ alert("Welcome to InterviewBit jQuery Interview Questions"); }); }); The live() method does not work in the given code, but we can easily do this using the delegate() function. So the code will be like this - $(document).ready(function(){$(".interviewbit")children("p").delegate("a","click", function(){ alert("you clicked on a link to jQuery interview questions."); }); }); |
|