Jquery Questions


jQuery Interview Questions :-

  What is jQuery?

jQuery is fast, lightweight and feature-rich client side JavaScript Library/Framework which helps in to traverse HTML DOM, make animations, add Ajax interaction, manipulate the page content, change the style and provide cool UI effect. It is one of the most popular client side library and as per a survey it runs on every second website.


  Why do we use jQuery?

Due to following advantages.

1. Easy to use and learn.

2. Easily expandable.

3. Cross-browser support (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)

4. Easy to use for DOM manipulation and traversal.

5. Large pool of built in methods.

6. AJAX Capabilities.

7. Methods for changing or applying CSS, creating animations.

8. Event detection and handling.

9. Tons of plug-ins for all kind of needs.


  Why jQuery is needed?

jQuery is needed for the following list:

1. Used to develop browser compatible web applications

2. Improve the performance of an application

3. Very fast and extensible

4. UI related functions are written in minimal lines of codes


  What is the basic need to start with jQuery?

To start with jQuery, one need to make reference of it's library.


  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.


  What is the advantage of using minimized version of jQuery?

Efficiency of web page increases when minimized version of jQuery is used.min.js file will be more than 50% less than the normal js file. Reduction in the file size makes the web page faster.


  Is jQuery is a JavaScript or JSON library file?

jQuery is a library of JavaScript file and it consists of DOM, event effects and the Ajax functions. jQuery is said to be a single JavaScript file.


  How JavaScript and jQuery are different?

JavaScript is a language While jQuery is a library built in the JavaScript language that helps to use the JavaScript language.


  In what scenarios jQuery can be used?

jQuery can be used in following scenarios:

1. Apply CSS static or dynamic

2. Calling functions on events

3. Manipulation purpose

4. Mainly for Animation effects


  What is the difference between find and children methods?

Find method is used to find all levels down the DOM tree but children find single level down the DOM tree.


  What is jQuery connect?

A 'jQuery connect' is a plugin used to connect or bind a function with another function. Connect is used to execute function from any other function or plugin is executed.


  How to use connect?

Connect can be used by downloading jQuery connect file from jQuery.com and then include that file in the HTML file.

Use $.connect function to connect a function to another function.


  Is jQuery replacement of Java Script?

No. jQuery is not a replacement of JavaScript. jQuery is a different library which is written on top of JavaScript.

jQuery is a lightweight JavaScript library that emphasizes interaction between JavaScript and HTML.

  Is there any difference between body onload() and document.ready() function?

document.ready() function is different from body onload() function for 2 reasons.

1. We can have more than one document.ready() function in a page where we can have only one body onload function.

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


  What is the difference between .js and .min.js?

jQuery library comes in 2 different versions Development and Production/Deployment. The deployment version is also known as minified version.

So .min.js is basically the minified version of jQuery library file. Both the files are same as far as functionality is concerned. but .min.js is quite small in size so it loads quickly and saves bandwidth.


  Why there are two different version of jQuery library?

jQuery library comes in 2 different versions.

1. Development

2. Production/Deployment

The development version is quite useful at development time as jQuery is open source and if you want to change something then you can make those changes in development version.

But the deployment version is minified version or compressed version so it is impossible to make changes in it. Because it is compressed, so its size is very less than the production version which affects the page load time.


  What is a CDN?

A content delivery network or content distribution network (CDN) is a large distributed system of servers deployed in multiple data centers across the Internet.

The goal of a CDN is to serve content to end-users with high availability and high performance.


  Which are the popular jQuery CDN? and what is the advantage of using CDN?

There are 3 popular jQuery CDNs.

1. Google.

2. Microsoft

3. jQuery.

Advantage of using CDN.

. It reduces the load from your server.

. It saves bandwidth. jQuery framework will load faster from these CDN.

. The most important benefit is it will be cached, if the user has visited any site which is using jQuery framework from any of these CDN


  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:

1. Name: Selects all elements which match with the given element Name.

2. #ID: Selects a single element which matches with the given ID

3. .Class: Selects all elements which match with the given Class.

4. Universal (*): Selects all elements available in a DOM.

5. Multiple Elements E, F, G: Selects the combined results of all the specified selectors E, F or G.

6. Attribute Selector: Select elements based on its attribute value.


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


  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.


  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.


  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.


  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.


  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.


  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.


  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)

1. styles: Specifies one or more CSS properties/values to animate.

2. duration: Optional. Specifies the speed of the animation.

3. easing: Optional. Specifies the speed of the element in different points of the animation. Default value is "swing".

4. callback: Optional. A function to be executed after the animation completes.

Simple use of animate function is,

$("btnClick").click(function(){
$("#dvBox").animate({height:"100px"});
});


  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.


  What is the difference between .empty(), .remove() and .detach() methods in jQuery?

All these methods .empty(), .remove() and .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(): Use .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.


  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.


  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(): 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 prop() to get values of elements which is modified via javascript/jquery , as it gives you the original value of an element's current state. Find out more here.


  What is event.PreventDefault?

The event.preventDefault() method stops the default action of an element from happening. For example, Prevents a link from following the URL.


  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.


  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.


  You get "jquery is not defined" or "$ is not defined" error. What could be the reason?

There could be many reasons for this.

1. You have forgot to include the reference of jQuery library and trying to access jQuery.

2. You have include the reference of the jQuery file, but it is after your jQuery code.

3. The order of the scripts is not correct. For example, if you are using any jQuery plugin and you have placed the reference of the plugin js before the jQuery library then you will face this error.


  What are various methods to make ajax request in jQuery?

Using below jQuery methods, you can make ajax calls.

1. load() : Load a piece of html into a container DOM

2. $.getJSON(): Load JSON with GET method.

3. $.getScript(): Load a JavaScript file.

4. $.get(): Use to make a GET call and play extensively with the response.

5. $.post(): Use to make a POST call and don't want to load the response to some container DOM.

6. $.ajax(): Use this to do something on XHR failures, or to specify ajax options (e.g. cache: true) on the fly.


  Is there any advantage of using $.ajax() for ajax call against $.get() or $.post()?

By using jQuery post()/ jQuery get(), you always trust the response from the server and you believe it is going to be successful all the time. Well, it is certainly not a good idea to trust the response. As there can be n number of reason which may lead to failure of response.

Where jQuery.ajax() is jQuery's low-level AJAX implementation. $.get and $.post are higher-level abstractions that are often easier to understand and use, but don't offer as much functionality (such as error callbacks).


  Can we execute/run multiple Ajax request simultaneously in jQuery? If yes, then how?

Yes, it is possible to execute multiple Ajax request simultaneously or in parallel. Instead of waiting for first ajax request to complete and then issue the second request is time consuming. The better approach to speed up things would be to execute multiple ajax request simultaneously.

Using jQuery .when() method which provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.


  How to use migrate jQuery plugin?

with release of 1.9 version of jQuery, many deprecated methods were discarded and they are no longer available. But there are many sites in production which are still using these deprecated features and it's not possible to replace them overnight. So jQuery team provided with jQuery Migrate plugin that makes code written prior to 1.9 work with it.

So to use old/deprecated features, all you need to do is to provide reference of jQuery Migrate Plugin.


  What is the advantage of using protocol less URL while referencing jQuery from CDNs?

It is quite useful when you are moving from HTTP to HTTPS url. You need to make sure that correct protocol is used for referencing jQuery library as pages served via SSL should contain no references to content served through unencrypted connections.

"protocol-less" URL is the best way to reference third party content that's available via both HTTP and HTTPS. When a URL's protocol is omitted, the browser uses the underlying document's protocol instead.


  What is jQuery plugin and what is the advantage of using plugin?

A plug-in is piece of code written in a standard JavaScript file. These files provide useful jQuery methods which can be used along with jQuery library methods.

jQuery plugins are quite useful as its piece of code which is already written by someone and re-usable, which saves your development time.


  What is jQuery UI?

jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library that can be used to build interactive web applications.


Hello!

Let's do interview preparation for node.js..

Read more