Jquery Interview Questions With Answers - Frequently Asked Questions

1)What is jQuery ?
It's very simple but most valuable Question on jQuery means jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, animating, event handling, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript. Jquery is build library for javascript no need to write your own functions or script jquery all ready done for you

Jquery Interview Questions With Answers

2 )How you will use Jquery means requirement needed for using jquery ?
Nothing more need to do just olny download jquery library(.js file) from any of the jquery
< script src="jquery.js" language="javascript" type="text/javascript">


3)what the use of $ symbol in Jquery?
$ Symbol is just replacement of jquery means at the place of $ you may use jquery hence $ symbol is used for indication that this line used for jquery


4)How do you select an item using css class or ID and get the value by use of jquery?
If an element of html like < div> , < p> or any tag have ID MyId and class used MyClass then we select the element by below jquery code
$('#MyId') for ID and for classs $('.MyClass') and for value
var myValue = $('#MyId').val(); // get the value in var Myvalue by idOr for set the value in selected item
$('#MyId').val("print me"); // set the value of a form input


5) How to get the server response from an AJAX request using Jquery?
When invoking functions that have asynchronous behavior We must provide a callback function to capture the desired result. This is especially important with AJAX in the browser because when a remote request is made, it is indeterminate when the response will be received.
$.ajax({
     url: 'pcdsEmpRecords.php',
     success: function(response) {
        alert(response);
     },
     error: function(xhr) {
        alert('Error!  Status = ' + xhr.status);
     }
 });


6)How do you update ajax response with id " resilts"
By using below code we can update div content where id 'results' with ajax response
 function updateStatus() {
     $.ajax({
            url: 'pcdsEmpRecords.php',
            success: function(response) {
             // update div id Results
             $('#results').html(response);
         }
     });
 }


7) How do You disable or enable a form element?
There are two ways to disable or enable form elements.
Set the 'disabled' attribute to true or false:
 // Disable #pcds
 $('#pcds').attr('disabled', true);
 // Enable #pcds
 $('#pcds').attr('disabled', false);
Add or remove the 'disabled' attribute:
 // Disable #pcds
 $("#pcds").attr('disabled', 'disabled');
 // Enable #x
 $("#pcds").removeAttr('disabled');


8)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.


9)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.
10)Is jQuery a library for client scripting or server scripting?
Client side scripting.
11)Is jQuery a W3C standard?
No. jQuery is not a W3C standard.


12)What is the basic need to start with jQuery?
To start with jQuery, one need to make reference of it's library. The latest version of jQuery can be downloaded from jQuery.com.


13)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.


14)What does dollar sign ($) means in jQuery?
Dollar Sign is nothing but it's an alias for JQuery. Take a look at below jQuery code.
$(document).ready(function(){
});
Over here $ sign can be replaced with "jQuery" keyword.
jQuery(document).ready(function(){
});


15)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.



16)Can we use our own specific character in the place of $ sign in jQuery?
Yes. It is possible using jQuery.noConflict().


17)Is it possible to use other client side libraries like MooTools, Prototype along with jQuery?
Yes


18)What is jQuery.noConflict?
As other client side libraries like MooTools, Prototype can be used with jQuery and they also use $() as their global function and to define variables. This situation creates conflict as $() is used by jQuery and other library as their global function. To overcome from such situations, jQuery has introduced jQuery.noConflict().
jQuery.noConflict();
// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
   jQuery("div").hide();
});
You can also use your own specific character in the place of $ sign in jQuery.
var $j = jQuery.noConflict();
// Use jQuery via jQuery(...)
$j(document).ready(function(){
   $j("div").hide();
});


19)Is there any difference between body onload() and document.ready() function?
ocument.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 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.



20)What is the difference between .js and .min.js?
jQuery library comes in 2 different versions Production and 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.


21)Why there are two different version of jQuery library?
jQuery library comes in 2 different versions.
Production
Deployment
The production 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 production 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.


22) 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.


23) 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


24) How to load jQuery from CDN?
Below is the code to load jQuery from all 3 CDNs.
Code to load jQuery Framework from Google CDN
<script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
Code to load jQuery Framework from Microsoft CDN
<script type="text/javascript"
    src="http://ajax.microsoft.com/ajax/jquery/jquery-1.9.1.min.js">
</script>
Code to load jQuery Framework from jQuery Site(EdgeCast CDN)
<script type="text/javascript"
    src="http://code.jquery.com/jquery-1.9.1.min.js">
</script>



25) What does $("div") will select?
This will select all the div elements on page.
25)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).


26)What does $("div.parent") will select?
All the div element with parent class.


27)What are the fastest selectors in jQuery?
ID and element selectors are the fastest selectors in jQuery.


28)What are the slow selectors in jQuery?
 class selectors are the slow compare to ID and element.


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


30)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.



31)What is the difference between jquery.size() and jquery.length?
 $('<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.


32)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.


33)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.


34)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:
$("btnClick").click(function(){
  $("#dvBox").animate({height:"100px"});
});


35)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


36)How do you stop the currently-running animation?
Using jQuery ".stop()" method.


37)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.


38)What is wrong with this code line "$('#myid.3').text('blah blah!!!');"
The problem with above statement is that the selectors is having meta characters and to use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\. For example, an element with id="foo.bar", can use the selector $("#foo\\.bar").
So the correct syntax is,
$('#myid\\.3').text('blah blah!!!');


39) What is event.PreventDefault?
e.preventDefault() will prevent the default event from occurring, e.stopPropagation() will prevent the event from bubbling up and return false will do both.


40)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.
$(document).ready(function(){
    $('#dvContent').addClass('dummy');
    $('#dvContent').css('color', 'red');
    $('#dvContent').fadeIn('slow');
});


41)How to write browser specific code using jQuery?
Using jQuery.browser property, we can write browser specific code. This property contains flags for the useragent, read from navigator.userAgent. This property was removed in jQuery 1.9.


42) Can we use jQuery to make ajax request?
Yes. jQuery can be used for making ajax request.


43)What are various methods to make ajax request in jQuery?
Using below jQuery methods, you can make ajax calls.
    load() : Load a piece of html into a container DOM
    $.getJSON(): Load JSON with GET method.
    $.getScript(): Load a JavaScript file.
    $.get(): Use to make a GET call and play extensively with the response.
    $.post(): Use to make a POST call and don't want to load the response to some container DOM.
    $.ajax(): Use this to do something on XHR failures, or to specify ajax options (e.g. cache: true) on the fly.


44)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 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.


45)What is the difference between .js and .min.js?
jQuery library comes in 2 different versions Production and 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.


46)Explain the features of jQuery.

    Effects and animations
    Ajax
    Extensibility
    DOM element selections functions
    Events
    CSS manipulation
    Utilities - such as browser version and the each function.
    JavaScript Plugins
    DOM traversal and modification.


48)Explain the concepts of "$ function" in jQuery with an example
The type of a function is "function".
There are a lot of anonymous functions is jquery.

$(document).ready(function() {});
$("a").click(function() {});

$.ajax({
     url: "someurl.php",
     success: function() {}
});


49)Explain how jQuery Works.
<html>
     <head>
     <script type="text/javascript" src="jquery.js"></script>
     <script type="text/javascript">
         // You can write the code here
     </script>
</head>
<body>
         <a href="http://careerride.com/">CareerRide</a>
</body>
</html>


50)When can you use jQuery?
jQuery can be used to for developing ajax based applications
It can be used to keep the code simple, concise and reusable.
It simplifies the process of traversal of HTML DOM tree.

It can also handle events, perform animation, and add the ajax support in web applications

Ad Inside Post

Comments system

Disqus Shortname