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:
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(){
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.
$('#spnValue').mouseover(function(){ alert($(this).text()); }); }); $(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(){
And the second way is using the "$.trim()" method.
if ($('#element').is(':empty')){ //Element is empty } }); $(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 $('
|