Latest jQuery interview questions and answers 04

Q31. How do you check if an element is empty?
Ans: There are 2 ways to check if element is empty or not. We can check using “:empty” selector.

1 $(document).ready(function(){
2     if ($('#element').is(':empty')){
3        //Element is empty
4   }
5 });

And the second way is using the “$.trim()” method.

1 $(document).ready(function(){
2      if($.trim($('#element').html())=='') {
3        //Element is empty
4   }
5 });

Q32. How do you check if an element exists or not in jQuery?
Ans: Using jQuery length property, we can ensure whether element exists or not.

1 $(document).ready(function(){
2     if ($('#element').length > 0){
3        //Element exists
4   });
5 });

Q33. What is the use of jquery .each() function?
Ans: 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.

Q34. 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.

Q35. 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.

Q36. 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.

Q37. 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. Find out more here.

Q38. 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.

Syntax is:

1 (selector).animate({styles},speed,easing,callback)
  • 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,

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

Q39. 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.

Q40. How do you stop the currently-running animation?
Ans: Using jQuery “.stop()” method.