Jul 26, 2015

jQuery Interview Questions - Part 3

Q: What is the use of jquery .each() function?
The $.each() function is used to iterate over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is an object or an array.

Q: What is the difference between jquery.size() and jquery.length?
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.

Q: What is the difference between $('div') and $('') in jQuery?
  • $(''): 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.

Q: What is the difference between parent() and parents() methods in jQuery?
The basic difference is the parent() function travels only one level in the DOM tree, where parents() function search through the whole DOM tree.

Q: What is the difference between .empty(), .remove() and .detach() methods in jQuery?
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.

Q: Explain .bind() vs .live() vs .delegate() vs .on()
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.

Q: How can we give face effect in jQuery?
In jQuery we have three methods to give the fade effect to elements:
fadeIn, fadeOut and fadeTo
This methods change the opacity of element with animation.
  • speed can be one of following values : “slow”, “fast”, “normal” or milliseconds
  • opacity specify the value that allows the fading to given opacity.
  • callback is the function which we want to run once the fading effect is complete.


Q: Explain the animate function.
The animate function is used to apply the custom animation effect to elements.

$(selector).animate({params}, [duration], [easing], [callback])
  • param defines the CSS properties on which you want to apply the animation.
  • duration: specify how long the animation will run. It can be one of following values : “slow”, “fast”, “normal” or milliseconds
  • easing is the string which specify the function for the transition.
  • callback: is the function which we want to run once the animation effect is complete.
Following is the jQuery to animate opacity, left offset, and height of the mydiv element
$('# clickToAnimate’).click(function(){
      opacity: 0.30,
      left: '+=20',
      height: 'toggle'
   }, 3000, function() {
      // run after the animation complete.

Q: Explain width() vs css(‘width’).
In jQuery, there are two way to change the width of an element.
One way is using .css(‘width’) and other way is using .width().

The difference in .css(‘width’) and .width() is the data type of value we specify or return from the both functions.
In .css(‘width’) we have to add “px” in the width value while in .width() we don’t have to add.
When you want to get the width of “mydiv” element then .css(‘width’) will return ‘300px’ while .width() will return only integer value 300.

Q: What is the use of jQuery.data()?
jQuery.data() is used to set/return arbitrary data to/from an element.
jQuery.data(element, key, value)
  • element is the DOM element to which the data is associated.
  • key is an arbitrary name of the piece of data.
  • value is value of the specified key.
Suppose we want to set the data for a span element:
jQuery.data(span, “item”, { val1: 10, val2: "myitem" });
If we want to retrieve the data related to div element and set it to label’s data:
$("label:val1").text(jQuery.data(div, "item").val1);
$("label:val2").text(jQuery.data(div, "item").val2);

<< Prev Home

    Choose :
  • OR
  • To comment
No comments:
Write Comments