Home
Jquery
Interview FAQs

FAQs of level

class selectors are the slow compare to ID and element.

Your last selector 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");

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 not use jQuery method and use the native JavaScript to get the value of span element.
$(document).ready(function(){
$("#spnValue").mouseover(function(){
alert(this.innerText);

});
});

Native JavaScript 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.

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.

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
});
});

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.

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

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

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 not be used. 

The basic difference is the parent() function travels only one level in the DOM tree, where parents() function search through the whole DOM tree.
PreviousDisplaying 5 of 7Next
1 2 3 4 5 6 7
Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*