Tuesday, May 27, 2014

JQuery Interview question and answer with Practices: Part 3

clip_image001

Kindly visit first part of JQuery interview questions and answer.

jQuery Interview Question and Answers With Practices: Part 1

jQuery Interview Questions and Answers With Practices: Part 2

clip_image003 Differences Between jQuery .bind() vs .live() vs .delegate()?

clip_image005 .bind()

This is the most straight forward binding method. jQuery scans the document for all $('a') elements and binds the alert function to each of their click events.

This is the code segment which I am about to use in my example as shown below in image.

clip_image007

I’ve bind the click event on selector.

clip_image009

Whenever you click on any anchor link it prompts you the message as shown below.

clip_image011

The bind() method registers the type of event and an event handler directly to the DOM elements. This method basically searches for all the elements and binds the same handler to all the matches found.it doesn’t give you problem if matched elements are less however if there are plenty of matched elements than it raises some efficiency issue to attach(bind) event handler to all matched elements. It doesn't work for elements added dynamically that matches the same selector, because when bind scans the DOM tree the element must be present in the tree otherwise the handler will not be applied.

After clicking on each link it adds new link .To verify whether bind() works as expected on new added element, please click on that element.

Kindly refer the images below:

clip_image013

clip_image014

Note: if you click on the newly added link it doesn’t prompt you alert message.

.live()

jQuery binds the alert function to the $(document) element, along with 'click' and 'a' as parameters. Any time an event bubbles up to the document node, it checks to see if the event was a click and if the target element of that event matches the 'a'  selector. If both are true, the function executes.

The good thing about this code as compared to the .bind() example above is that it is only attaching the event handler once to the document instead of multiple times. This not only is faster, but less wasteful. It also attaches the event to dynamically added elements. Because the real information was registered on the document.it also doesn’t support chaining.

After clicking on each link it adds a new link dynamically, which prompts you alert.

A quick example of adding elements at runtime with an example shown below:

clip_image016

clip_image017

Note: if you click on the newly added link it prompts you alert message.

.delegate()

This function works in similar manner to live() function but instead of attaching event/selector information to document ,you can attach to element you desire.

Ref: http://api.jquery.com/delegate/

.delegate( selector, eventType, handler )Returns: jQuery

Description: Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.

· Delegates is faster/efficient than the live function because delegate is attached to specific root element rather than the document along with the selector/event information.

· You have the option of choosing where to attach the selector/event information.

· Chaining is better than live().

· Note: Chaining is not possible in live().

Below is the very descriptive image of delegates function and related terminology.

 

clip_image019

 

Note: if you click on the newly added link it prompts you alert message.

 

clip_image020

Note: on() is a replacement of all above method introduced in jQuery 1.7.1 version.in short I’d say it’s an integration of all above method.

clip_image021 What is event handler?

clip_image022I’ve used a term event handler mostly in above detailed description so I thought to lit on this also. “An Event handler is a code segment attached to an element which executes, when an event occurs on that element”.

Kindly have a look on shown below image:

clip_image023

clip_image003[1] Difference between Filter and Find?

clip_image005[1]As far as my understanding with these both perform action in similar way.

But filter searches all elements on basis of selector value, though find applies on child elements only.

Kindly have a look on a very descriptive image as shown below:

clip_image025

Hope you enjoyed this demonstration.

ThanksSmile

Keep coding and Smile Smile

0 comments :

Post a Comment