Wednesday, 4 December 2013

jQuery Interview Question and Answer - Part-3

Q31. What is the difference between jquery.size() and jquery.length?

Ans: jQuery.size() method returns number of element in the object. But it is not preferred to use the size()method as jQuery provide .length property and which does the same thing. But the .length property is preferred because it does not have the overhead of a function call.

Q32. What is the difference between $('div') and $('<div/>') in jQuery?

Ans: $('<div/>') : This creates a new div element. However this is not added to DOM tree unless you don't append it to any DOM element.

$('div') : This selects all the div element present on the page.

Q33. What is the difference between parent() and parents() methods in jQuery?

Ans: The basic difference is the parent() function travels only one level in the DOM tree, where parents() function search through the whole DOM tree.

Q34. What is the difference between eq() and get() methods in jQuery?

Ans: eq() returns the element as a jQuery object. This method constructs a new jQuery object from one element within that set and returns it. That means that you can use jQuery functions on it. get() return a DOM element. The method retrieve the DOM elements matched by the jQuery object. But as it is a DOM element and it is not a jQuery-wrapped object. So jQuery functions can't be used. 

Q35. How do you implement animation functionality? 

Ans: The .animate() method allows us to create animation effects on any numeric CSS property. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect.


• styles: Specifies one or more CSS properties/values to animate. 
• duration: Optional. Specifies the speed of the animation. 
• easing: Optional. Specifies the speed of the element in different points of the animation. Default value is "swing". 
• callback: Optional. A function to be executed after the animation completes. Simple use of animate function is, 

$("btnClick").click(function(){ $("#dvBox").animate({height:"100px"}); }); 

Q36. How to disable jQuery animation?
Ans: Using jQuery property "", which when set to true, disables all the jQuery animation. When this is done, all animation methods will immediately set elements to their final state when called, rather than displaying an effect.

Q37. How do you stop the currently-running animation?
Ans: Using jQuery ".stop()" method.

Q38. What is the difference between .empty(), .remove() and .detach() methods in jQuery?

Ans: All these methods .empty(), .remove() and .detach() are used for removing elements from DOM but they all are different. .empty(): This method removes all the child element of the matched element where remove() method removes set of matched elements from DOM. .remove(): Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. .detach(): This method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

Q39. Explain .bind() vs .live() vs .delegate() vs .on() 

Ans: All these 4 jQuery methods are used for attaching events to selectors or elements. But they all are different from each other. .bind(): This is the easiest and quick method to bind events. But the issue with bind() is that it doesn't work for elements added dynamically that matches the same selector. bind() only attach events to the current elements not future element. Above that it also has performance issues when dealing with a large selection. .live(): This method overcomes the disadvantage of bind(). It works for dynamically added elements or future elements. Because of its poor performance on large pages, this method is deprecated as of jQuery 1.7 and you should stop using it. Chaining is not properly supported using this method. .delegate(): The .delegate() method behaves in a similar fashion to the .live() method, but instead of attaching the selector/event information to the document, you can choose where it is anchored and it also supports chaining. .on(): Since live was deprecated with 1.7, so new method was introduced named ".on()". This method provides all the goodness of previous 3 methods and it brings uniformity for attaching event handlers.

Q40. What is wrong with this code line "$('#myid.3').text('blah blah!!!');" 

Ans: The problem with above statement is that the selectors is having meta characters and to use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\. For example, an element with id="", can use the selector $("#foo\\.bar"). So the correct syntax is, $('#myid\\.3').text('blah blah!!!');

Q41. How to create clone of any object using jQuery?

Ans: jQuery provides clone() method which performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes. 

$(document).ready(function(){ $('#btnClone').click(function(){ $('#dvText').clone().appendTo('body'); return false; }); });

Q42. Does events are also copied when you clone any element in jQuery?

Ans: As explained in previous question, using clone() method, we can create clone of any element but the default implementation of the clone() method doesn't copy events unless you tell the clone() method to copy the events. The clone() method takes a parameter, if you pass true then it will copy the events as well. 

$(document).ready(function(){ $("#btnClone").bind('click', function(){ $('#dvClickme').clone(true).appendTo('body'); }); 

Q43. What is difference between prop and attr? 

Ans: attr(): Get the value of an attribute for the first element in the set of matched elements. Whereas,.prop(): (Introduced in jQuery 1.6) Get the value of a property for the first element in the set of matched elements. Attributes carry additional information about an HTML element and come in name="value" pairs. Where Property is a representation of an attribute in the HTML DOM tree. once the browser parse your HTML code ,corresponding DOM node will be created which is an object thus having properties. attr() gives you the value of element as it was defines in the html on page load. It is always recommended to useprop() to get values of elements which is modified via javascript/jquery , as it gives you the original value of an element's current state.

Q44. What is event.PreventDefault?

Ans: The event.preventDefault() method stops the default action of an element from happening. For example, Prevents a link from following the URL.

Q45. What is the difference between event.PreventDefault and event.stopPropagation?

Ans: event.preventDefault(): Stops the default action of an element from happening. event.stopPropagation(): Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. For example, if there is a link with a click method attached inside of a DIV or FORM that also has a click method attached, it will prevent the DIV or FORM click method from firing.

PREVIOUS                                                                                                                                 NEXT

No comments:

Post a Comment