Real Time Jquery Interview Questions with Answers PDF
1. What is jQuery?
jQuery is a light weight JavaScript library which gives quick and simple method for HTML DOM traversing and manipulation,its event handling,its client side animations,and so on. One of the best feature of jQuery is that jQuery supports an efficient way to implement AJAX applications because of its light weight nature and make normalize and efficient web programs.
2. How is body onload() function is different from document.ready() function used in jQuery?
Document.ready() function is different from body onload() function because off 2 reasons.
• We can have more than one document.ready() function in a page where we can have only one onload function.
• Document.ready() function is called as soon as DOM is loaded where body.onload() function is called when everything gets loaded on the page that includes DOM, images and all associated resources of the page.
3. What are features of JQuery or what can be done using JQuery?
Features of Jquery
• One can easily provide effects and can do animations.
• Applying / Changing CSS.
• Cool plugins.
• Ajax support
• DOM selection events
• Event Handling
4. What are the different type of selectors in Jquery?
There are 3 types of selectors in Jquery
• CSS Selector
• XPath Selector
• Custom Selector
5. What are the advantages of JQuery ?
There are many advantages with JQuery. Some of them are :
• It is more like a JavaScript enhancement so there is no overhead in learning a new syntax.
• It has the ability to keep the code simple, readable, clear and reusable.
• Cross-browser support (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)
• It would eradicate the requirement for writing complex loops and DOM scripting library calls.
• Event detection and handling.
• Tons of plug-ins for all kind of needs.
6. Why jQuery?
jQuery is very compact and well written JavaScript code that increases the productivity of the developer by enabling them to achieve critical UI functionality by writing very less amount of code.
It helps to
• Improve the performance of the application
• Develop most browser compatible web page
• Implement UI related critical functionality without writing hundreds of lines of codes
• Fast
• Extensible – jQuery can be extended to implement customized behavior
Other advantages of jQuery are
• No need to learn fresh new syntax’s to use jQuery, knowing simple JavaScript syntax is enough
• Simple and Cleaner code, no need to write several lines of codes to achieve complex functionality.
7. What is jQuery Selectors? Give some examples
• jQuery Selectors are used to select one or a group of HTML elements from your web page.
• jQuery support all the CSS selectors as well as many additional custom selectors.
• jQuery selectors always start with dollar sign and parentheses: $()
There are three building blocks to select the elements in a web document.
1. Select elements by tag name
Example
$(div)
It will select all the div elements in the document.
2. Select elements by ID
Example
$(“#abc”)
It will select single element that has an ID of abc.
3. Select elements by Class
Example
$(“.xyzClass”)
It will select all the elements having class xyzClass.
8. Explain width() vs css(‘width’)
In jQuery, there are two way to change the width of an element. One way is using .css(‘width’) and other way is using .width().
For example
$('#mydiv').css(‘width’,’300px’);
$('#mydiv').width(100);
• The difference in .css(‘width’) and .width() is the data type of value we specify or return from the both functions.
• In .css(‘width’) we have to add “px” in the width value while in .width() we don’t have to add.
• When you want to get the width of “mydiv” element then .css(‘width’) will return ‘300px’ while .width() will return only integer value 300.
9. Explain bind() vs live() vs delegate() methods.
The bind() method will not attach events to those elements which are added after DOM is loaded while live() and delegate() methods attach events to the future elements also.
The difference between live() and delegate() methods is live() function will not work in chaining. It will work only on an selector or an element while delegate() method can work in chaining.
For example
$(document).ready(function(){
$("#myTable").find("tr").live("click",function(){
alert($(this).text());
});
});
Above code will not work using live() method. But using delegate() method we can accomplish this.
$(document).ready(function(){
$("#dvContainer")children("table").delegate("tr","click",function(){
alert($(this).text());
});
});
10. What is the use of param() method.
The param() method is used to represent an array or an object in serialize manner.
While making an ajax request we can use these serialize values in the query strings of URL.
Syntax:
$.param(object | array, boolValue)
“object | array” specifies an array or an object to be serialized.
“boolValue” specifies whether to use the traditional style of param serialization or not.
Example
personObj=new Object();
empObject.name="Ravi";
empObject.age="28";
empObject.dept="IT";
$("#clickme").click(function(){
$("span").text($.param(empObject));
});
It will set the text of span to “name=Ravi&age=28&dep=IT”
11. 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.
12. How to read, write and delete cookies in jQuery?
To deal with cookies in jQuery we have to use the Dough cookie plugin.
Dough is easy to use and having powerful features.
• Create cookie:
$.dough(“cookie_name”, “cookie_value”);
• Read Cookie:
$.dough(“cookie_name”);
• Delete cookie:
$.dough(“cookie_name”, “remove”);
13. What is difference between $(this) and ‘this’ in jQuery?
$(document).ready(function(){
$(‘#clickme’).click(function(){
alert($(this).text());
alert(this.innerText);
});
});
this and $(this) references the same element but the difference is that “this” is used in traditional way but when “this” is used with $() then it becomes a jQuery object on which we can use the functions of jQuery.?
In the example given, when only “this” keyword is used then we can use the jQuery text() function to get the text of the element, because it is not jQuery object. Once the “this” keyword is wrapped in $() then we can use the jQuery function text() to get the text of the element.
14. What are the various ajax functions ?
Ajax allows the user to exchange data with a server and update parts of a page without reloading the entire page. Some of the functions of ajax are as follows:
$.ajax(): This is considered to be the most low level and basic of functions. It is used to send requests . This function can be performed without a selector.
$.ajaxSetup(): This function is used to define and set the options for various ajax calls.
For example.
$.ajaxSetup({
"type":"POST",
"url":"ajax.php",
"success":function(data){
$("#bar")
.css("background","yellow")
.html(data);
}
});
Shorthand ajax methods: They comprise of simply the wrapper function that call $.ajax() with certain parameters already set.
$.getJSON(): this is a special type of shorthand function which is used to accept the url to which the requests are sent. Also optional data and optional callback functions are possible in such functions.
15. Explain .empty() vs .remove() vs .detach().
• .empty() method is used to remove all the child elements from matched elements.
• .remove() method is used to remove all the matched element. This method will remove all the jQuery data associated with the matched element.
• .detach() method is same as .remove() method except that the .detach() method doesn’t remove jQuery data associated with the matched elements.
.remove() is faster than .empty() or .detach() method.
Syntax:
$(selector).empty();
$(selector).remove();
$(selector).detach();
16. How can events be prevented from stopping to work after an ajax request?
There are two ways to handle this issue:
Use of event delegation: The event delegation technique works on principle by exploiting the event bubbling. It uses event bubbling to capture the events on elements which are present anywhere in the domain object model. In jquery the user can make use of the live and die methods for the events delegation which contains a subset of event types.
For example. handling even delegation, handling of clicks on any <a> element:
$('#mydiv').click(function(e){
if( $(e.target).is('a') )
fn.call(e.target,e);
});
$('#mydiv').load('my.html')
Event rebinding usage: When this method is used it requires the user to call the bind method and the added new elements.
$('a').click(fn);
$('#mydiv').load('my.html',function(){
$('#mydiv a').click(fn);
});
No comments:
Post a Comment