The largest Interview Solution Library on the web


Interview Questions
« Previous | 0 | 1 | 2 | 3 | 4 | Next »

21.What are selectors in jQuery and how many types of selectors are there?

To work with an element on the web page, first we need to find them. To find the html element in jQuery we use selectors. There are many types of selectors but basic selectors are:

  • Name: Selects all elements which match with the given element Name.
  • #ID: Selects a single element which matches with the given ID
  • .Class: Selects all elements which match with the given Class.
  • Universal (*): Selects all elements available in a DOM.
  • Multiple Elements E, F, G: Selects the combined results of all the specified selectors E, F or G.
  • Attribute Selector: Select elements based on its attribute value.

22.How do you select element by ID in jQuery?

To select element use ID selector. We need to prefix the id with "#" (hash symbol). For example, to select element with ID "txtName", then syntax would be,

$('#txtName')

23.What does $("div") will select?

This will select all the div elements on page.

24.How to select element having a particular class (".selected")?

$('.selected'). This selector is known as class selector. We need to prefix the class name with "." (dot).

25.What does $("div.parent") will select?

All the div element with parent class.

26.What are the fastest selectors in jQuery?

ID and element selectors are the fastest selectors in jQuery.

27.What are the slow selectors in jQuery?

class selectors are the slow compare to ID and element.

28.How jQuery selectors are executed?

Your last selectors is always executed first. For example, in below jQuery code, jQuery will first find all the elements with class ".myCssClass" and after that it will reject all the other elements which are not in "p#elmID".

$("p#elmID .myCssClass");

29.Which is fast document.getElementByID('txtName') or $('#txtName').?

Native JavaScipt is always fast. jQuery method to select txtName "$('#txtName')" will internally makes a call to document.getElementByID('txtName'). As jQuery is written on top of JavaScript and it internally uses JavaScript only So JavaScript is always fast.

30.Difference between $(this) and 'this' in jQuery?

this and $(this) refers to the same element. The only difference is the way they are used. 'this' is used in traditional sense, when 'this' is wrapped in $() then it becomes a jQuery object and you are able to use the power of jQuery.

$(document).ready(function(){
$('#spnValue').mouseover(function(){
alert($(this).text());
});
});
In below example, this is an object but since it is not wrapped in $(), we can't use jQuery method and use the native JavaScript to get the value of span element.
$(document).ready(function(){
$('#spnValue').mouseover(function(){
alert(this.innerText);
});
});

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

$(document).ready(function(){
if ($('#element').is(':empty')){
//Element is empty
}
});
And the second way is using the "$.trim()" method.
$(document).ready(function(){
if($.trim($('#element').html())=='') {
//Element is empty
}
});

32.How do you check if an element exists or not in jQuery?

Using jQuery length property, we can ensure whether element exists or not.

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

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

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

35.What is the difference between $('div') and $('
') 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.

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

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

38.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:

(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,
$("btnClick").click(function(){
$("#dvBox").animate({height:"100px"});
});

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

40.How do you stop the currently-running animation?

Using jQuery ".stop()" method.

« Previous | 0 | 1 | 2 | 3 | 4 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com