jQuery Interview Questions and Answers

What is jQuery?

jQuery is an open source JavaScript library that simplify Html document traversing and manipulation browser event handling, DOM animations, Ajax interaction and cross bowser JavaScript development.

How jQuery differ from JavaScript?

jQuery is just a JavaScript library and has built in JavaScript code functionality that optimized development, reduce effort and provided cross browsers supports.

Why jQuery and features of jQuery?

jQuery philosophy is “Write less, do more”

Open source and has large community of users and contributors

Tested and optimized for modern browsers

Lightweight, robust and expandable

Offers plug-ins and ready-made functionality

Can be used for event-driven programming

AJAX support

How we can use jQuery on a web page?

We can use jQuery in web page by two way

Use of content delivery network (CDN) to include a version of jQuery.
Downloaded version of jQuery in local file-system from jQuery.com.

What is a CDN? Why a CDN is Useful to jQuery and advantage of it?

Content delivery network is geographically distributed worldwide high-speed collection of servers, which can serve content to end-users in lowround-trip times.

Advantage:

We can start using jQuery from CDN simply by adding a <script> tag in web page without locally downloaded.

Browsers reused cached jQuery files for web sites that are located in different domains, which significantly improve performance.

CDN offers reliable high-speed access on jQuery library withminimum round-trip time.

Which are the popular jQuery CDN? How to load jQuery from CDN?

Google, Microsoft and jQuery are popular jQuery CDN.

To load a hosted library from CDN, copy and paste the below HTML snippet for that library (shown below) in your web page

Google (https://developers.google.com/speed/libraries/#web-font-loader)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>

Microsoft (http://www.asp.net/ajax/cdn)
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script>

JQuery (https://code.jquery.com/)
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

How to load jQuery locally when CDN unavailable?

To allow to loading jQuery from a local path on your own website if the CDN happens to be unavailable, add the following element immediately after the element referencing the CDN:

<script src=" /jquery/jquery-1.9.0.min.js"></script>

Ex:

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script>

<script src=" /jquery/jquery-1.9.0.min.js"></script>

What is the difference between jQuery and its alias $?

jQuery and $ are the same thing and work same. $ is just a shortcut.

For example, the following statement will create an <a>element wrapped inside of a <p>element with a text node encapsulated inside of the <p>and <a>elements:

jQuery('<p><a>jQuery</a></p>');
which is the same as
$('<p><a>jQuery</a></p>');

Can we replace any character or variable instead of $ in jQuery?

We can replace $ by using no Conflict () method, see below snippet to replace $ to jqwrite

var jqwrite = $.noConflict()

Explained jQuery document.ready() function?

Everything inside the $(document).ready() function will load as soon as the DOM (Document Object Model) is loaded and before the page contents are loaded.

The ready() event handler method is jQuery’s replacement for using the JavaScript core window.onloadevent.

jQuery Document Ready Snippet
$(document).ready(function() {  // Put javascript here});

Different ways to write jQuery Document Ready Snippet
$(document).ready(function() { ... });
$(function() { ... });
jQuery(document).ready(function() { ... });
jQuery(function() { ... });

jQuery document ready function can be used as many times in page as you like.

What is the difference between JavaScript window.onload() event and jQuery ready()functions?

jQuery ready() function only waits for the DOM tree to be loaded before executing the code. Where JavaScript onload event not only waits for DOM to be created but also waits for all external resources to load fully including heavy images, audios and videos etc.
If loading images and other third party elements takes significant amount of time then user can experience significantly be impacted on execution of code defined in window.onloadevent.

As mentioned above jQuery ready() function recommended over JavaScript onload event. However, there are valid use cases that justifies use of JavaScript onload over jQuery ready().

What are selectors in jQuery and how many types of selectors are there?

jQuery selectors are used to select html elements to in order to operate on the element based on their id, class, element type, attributes, and many more. There are many types of selectors but important selectors are:

Element Selector (“element”):Selects all elements with the given tag name
$(“div”).css(“color”,”red”);// text color of all div elements will be red

All Selector (“*”): select all HTML elements at a time.
$(“*”).css(“color”, “blue”);//change all elements text color to blue

Id Selector (“#id”): Selects a single element with the given id attribute.
$(“#id”).css(“background”, “black”);

Class Selector (“.class”): Selects all elements with the given class.
$(“.class”).css(“color”, “black”);

Attribute Selector [name=”value”]: Select elements based on its attribute value.

Multiple Selector (“selector1, selector2, selectorN”): Select elements that match all selector condition.

Which are the performance responsive selector?

The elements which are selected using ID are the performance responsive selector as the ID is unique throughout the rendered page.

How do you call AJAX request using jQuery?

jQuery provided $.ajax({...}); function to support Ajax functionality.

Below an example of making an AJAX call and alerting the response (or error):

$.ajax({
url:'pAjaxReqServer.php',
success:function(response) {
alert(response);
       },
error:function(xhr) {
alert('Error!  Status = '+xhr.status);
       }
});
What is the difference between jQuery.ajax(),jQuery.get() and jQuery.post()?

jQuery.get() and jQuery.post() contain features that are subsets of jQuery.ajax().

jQuery.ajax()is the method that provides the most flexibility of the three.The biggest difference from an implementation point is that you can pass an objecttojQuery.ajax(), which contains the necessary parameters.WherejQuery.get()andjQuery.post() are both shorthand forjQuery.ajax().

Ex:jQuery.ajax()

jQuery.ajax({
url:'callAjax.php,
dataType:'text',
type:‘GET’,
success:function(response) {
alert(response);
  }
});

Ex:jQuery.get()

jQuery.get('callAjax.txt',function(response){ 
alert(response) 
},'text');

Ex:jQuery.post()

jQuery.post('callAjax.txt',function(response){
alert(response)
},'text');

Latest Updates

Physics

Atmospheric pressure at sea level equals ?

General Knowledge of India

Which of the following dance forms was introduced by the great Vaishnava saint and reformer Mahapurusha Sankaradeva ?

Which of the following is the only classical dance of india having links with Muslim culture and represents a unique synthesis of Hindu and Muslim genius in art ?

Time and Distance

Starting from the same point at the same time, Ruhan and Yuvan had to reach the same destination. While Ruhan drove at 45 km/hr, Yuvan was slower by 5 km/hr. If Yuvan arrived at the destination an hour after Ruhan did, the distance covered by each, in Kilometres, was ?

Simple Interest

A sum of money was invested at a certain rate of simple interest for 4 years. Had the sum been invested at a rate 3% higher than the actual rate, the interest earned would have been 720 Rs more. Find the sum initially invested?

Ratio and Proportion

A bag contains red, blue and black balls. The ratio of red to blue balls is 3 : 5. The ratio of black to red balls is 3 : 4. What is the ratio of blue to black balls?

Profit and Loss

Selling a pack of incense sticks for 136 Rs resulted in a loss of 15%. To make a profit of 10%, the selling price should be ?

Percentage

23% of a bucket full of diluted milk was water. If the quantity of pure milk in the bucket was 15.4 liter, then the capacity of the bucket was ?

Average

The average of the marks obtained by three girls in a test was twice the average of the marks obtained by eight boys in that test. If the total marks obtained by all these examinees was 168, find the average marks each of the girls obtained ?

Pipes and Cistern

44 pipes can fill a large water tank in 27 hours. How many hours it take for 66 pipes to fill four such tanks ?

HCF and LCM

A natural number, when divided by 3, 4, 5, 6, and 7 respectively, leaves corresponding remainder of 2, 3, 4, 5 and 6. What is the smallest of all such numbers fulfilling the above condition ?

Coding-Decoding

If in a certain code language, PRINT is written as 34825 and TRUE is written as 5479. Then, how will TENURE be written in that language?

Order Sequence

Arrange the given words from smallest to biggest-
1.Jug
2.Mug
3.Bucket
4.Drum
5.Cup

Statement and Conclusion

Statements
All socks are shoes
Some shoes are black

Conclusions
I. Some shoes are socks.
II. No scoks is black

Numbers

The difference between the place value of 4 and 2 in the number 833749502 is ?

Analogy

GIKM : HJLN :: PRTV : .........?

Order Sequence

In a row of girls, Nanda is eighth from the left and Chanda is nineteenth from right. When Nanda and Chanda interchange their positions, Chanda becomes twnty-third form right. How many girls are there in the row?

Analogy

Summer: Day :: Winter : ........?