Home Interview Questions and Answers Jquery Interview questions and Answers For Developers Part-4

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

$(document).ready(function(){
if ($(‘#element’).is(‘:empty’)){
//Element is empty
}
});

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

$(document).ready(function(){
if($.trim($(‘#element’).html())==”) {
//Element is empty
}
});  

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

33. How do you check if an element exists or not in jQuery?
Using jQuery length property, we can ensure whether element exists or not.
Hide   Copy Code

$(document).ready(function(){
if ($(‘#element’).length > 0){
//Element exists
}
});

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

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

36. What is the difference between $(‘div’) and $(‘<div/>’) in jQuery?
$(‘<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.

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

38. What is the difference between eq() and get() methods in jQuery?
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.

39. How do you implement animation functionality?
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:
Hide   Copy Code

(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,
Hide   Copy Code

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

40. How to disable jQuery animation?
Using jQuery property “jQuery.fx.off”, 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.

You may also like

Leave a Comment