1 . What is jQuery?
2 . Why do we use jQuery?
- Free Library, Easy to learn and use.
- Cross-browser support
- Easy to use for DOM manipulation and traversal
- Large pool of built in methods to traverse DOM
- Built in Ajax Capabilities
- Easy to apply CSS, creating animations
- Event detection and handling
3 . Is jQuery a programming language?
5 . Is jQuery a library for client scripting or server scripting?
6 . Is jQuery a W3C standard?
No , jQuery is not a W3C standard.
7 . Which is the starting point of code execution in jQuery?
The starting point of jQuery code execution is $(document).ready() function which is executed when DOM is loaded.
8 . Can we have multiple document.ready() function on the same page?
YES, We can have any number of document.ready() function on the same page.
9 . What does dollar sign ($) means in jQuery?
10 . Is it possible to use other client side libraries, Prototype along with jQuery?
11 . What is jQuery.noConflict?
12 . Can we use our own specific character in the place of $ sign in jQuery?
As seen above question Yes, It is possible using jQuery.noConflict().
13 . Is there any difference between body onload() and document.ready() function?
document.ready() function is different from body
onload() function for 2 reasons.
- We can have more than one
document.ready() function in a page where we can have only one body
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.
14 . Why there are two different version of jQuery library?
jQuery library comes in 2 different versions.
The development version is mostly used for debugging purpose and to add custom changes to test.
15 . 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.
16 . 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 "userName", then syntax would be,
17 . What does $("div") will select?
This will select all the div elements on page.
18 . 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).
19 . What does $("div.parent") will select?
All the div element with parent class.
20 . What are the fastest selectors in jQuery?
ID and element selectors are the fastest selectors in jQuery.
21 . What are the slow selectors in jQuery?
class selectors are the slow compare to ID and element.
22 . How jQuery selectors are executed?
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
24 . Difference between $(this) and 'this' in jQuery?
25 . How do you check if an element is empty?
26 . How do you check if an element exists or not in jQuery?
27 . What is the use of jquery .each() function?
$.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.
28 . What is the difference between jquery.size() and jquery.length?
.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.
29 . 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.
30 . 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.
31 . 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.
32 . 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.
Simple use of animate function is,
- 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.
33 . 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.
34 . How do you stop the currently-running animation?
Using jQuery "
35 . What is the difference between .empty(), .remove() and .detach() methods in jQuery?
All these methods
.detach() are used for removing elements from DOM but they all are different.
.empty(): This method removes all the child element of the matched element where remove() method removes set of matched elements from DOM.
.remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.
.detach(): This method is the same as
.remove(), except that
.detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.
36 . Explain .bind() vs .live() vs .delegate() vs .on()
All these 4 jQuery methods are used for attaching events to selectors or elements. But they all are different from each other.
.bind(): This is the easiest and quick method to bind events. But the issue with bind() is that it doesn't work for elements added dynamically that matches the same selector.
bind() only attach events to the current elements not future element. Above that it also has performance issues when dealing with a large selection.
.live(): This method overcomes the disadvantage of
bind(). It works for dynamically added elements or future elements. Because of its poor performance on large pages, this method is deprecated as of jQuery 1.7 and you should stop using it. Chaining is not properly supported using this method.
.delegate(): The .delegate() method behaves in a similar fashion to the .live() method, but instead of attaching the selector/event information to the document, you can choose where it is anchored and it also supports chaining.
.on(): Since live was deprecated with 1.7, so new method was introduced named ".on()". This method provides all the goodness of previous 3 methods and it brings uniformity for attaching event handlers.
37 . How to create clone of any object using jQuery?
38 . Do events are also copied when you clone any element in jQuery?
39 . What is difference between prop and attr?
attr(): Get the value of an attribute for the first element in the set of matched elements. Whereas,
.prop(): (Introduced in jQuery 1.6) Get the value of a property for the first element in the set of matched elements.
Attributes carry additional information about an HTML element and come in name="value" pairs. Where Property is a representation of an attribute in the HTML DOM tree. once the browser parse your HTML code ,corresponding DOM node will be created which is an object thus having properties.
attr() gives you the value of element as it was defines in the html on page load. It is always recommended to use
40 . What is event.PreventDefault?
event.preventDefault() method stops the default action of an element from happening. For example, Prevents a link from following the URL.
41 . What is the difference between event.PreventDefault and event.stopPropagation?
event.preventDefault(): Stops the default action of an element from happening.
event.stopPropagation(): Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. For example, if there is a link with a click method attached inside of a DIV or FORM that also has a click method attached, it will prevent the DIV or FORM click method from firing.
42 . What is the difference between event.PreventDefault and "return false"?
e.preventDefault() will prevent the default event from occurring,
e.stopPropagation() will prevent the event from bubbling up and return false will do both.
What is the difference between event.stopPropagation and event.stopImmediatePropagation?
44 . How to check if number is numeric while using jQuery?
isNumeric()" function which was introduced with jQuery 1.7.
45 . How to check data type of any variable in jQuery?
46 . How do you attach a event to element which should be executed only once?
47 . Can you include multiple version of jQuery? If yes, then how they are executed?
Yes. Multiple versions of jQuery can be included in same page.
48 . In what situation you would use multiple version of jQuery and how would you include them?
49 . Is it possible to hold or delay document.ready execution for sometime?
50 . What is chaining in jQuery?
Chaining is one of the most powerful feature of jQuery. In jQuery, Chaining means to connect multiple functions, events on selectors. It makes your code short and easy to manage and it gives better performance. The chain starts from left to right. So left most will be called first and so on.
The above jQuery code sample can be re-written using chaining. See below.
Not only functions or methods, chaining also works with events in jQuery.