What is Jquery?

A lightweight JavaScript library which gives a quick and simple method for HTML DOM traversing and manipulation, its event handling, its client-side animations, and so on.

Download JQuery Interview Questions PDF

JQuery Interview Questions

Posted in JavaScript

sharad
Sharad Jaiswal

Updated On 06 Feb, 2020

Comments: 20

Q1. What is jQuery?

jQuery is a lightweight JavaScript library which gives a quick and simple method for HTML DOM traversing and manipulation, its event handling, its client-side animations, and so on. One of the best features of jQuery is that jQuery supports an efficient way to implement AJAX applications because of its lightweight nature and make normalize and efficient web programs.

Q2. What is Jquery Connect?

The jQuery connect is a plugin that is used to bind or connect a function to another function. It executes a function when a function from another object is executed. It is similar to assigning a handler for another function. Connect also lets you connect a function to a DOM object. With connect, you bind more than one function.

Example

$.connect(obj1, 'fun1', onj1, fun2); 


Here we are binding fun2 function of object 2 to the fun1 function of object 1. So, when fun2 is executed, fun1 will also be executed.

Q3. What is use of $.noConflict?

The $.noConflict() is used to provide the $ variable to the library that is implemented first. It is used to resolve any conflicts between the $ objects of other libraries and $ variables that represent jQuery.

Example

$.noConflict 

Code that uses $ object of other libraries can be written here without any conflict between the $ (i.e), jQuery.

Q4. Is jQuery a W3C standard?

No. JQuery is not a W3C standard. It is just a JavaScript library that provides an abstraction to the functions executed in JavaScript.

Q5. What is the use of jQuery filter?

The filter method in the jQuery returns certain elements that match the specified criteria. You can specify a criterion with the filter method and apply it to DOM elements. It removes the elements that do not match the specified criteria and allows only that matches. It works like a search function.

Syntax

$(selector).filter(criteria,function(index)) 

Here the criteria specify an expression that you want to apply to the DOM elements for filtering. You can specify multiple criteria separated by a comma. The function is optional that runs on each specified element. It returns true if the element matches the criteria or false otherwise.

Q6. What are deferred and promise object in jQuery?

The deferred object in jQuery is used to create a promise. After creating a promise, jQuery changes its state to resolved or rejected. Deferred is used to provide a promise to the calling code in your own function.

The promise is set on a deferred object that will be executed when the deferred collection is complete. The state of the promise cannot be changed. The deferred object is used to create a promise.

Q7. What are the advantages of JQuery ?

There are many advantages of using 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.

Q8. Why jQuery is used?

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.

Q9. What is chaining in jQuery?

Chaining in jQuery lets you run multiple statements one after another on the same element. To chain multiple commands, append each jQuery command to the previous one using dot(.). Chaining allows you not to use one selector more than once for performing an action as all the actions are performed at once. It speeds up the time taken to execute the code as the compiler need not call a selector more than one time.

Example

$(“#h1”).css(“color”, "blue”).slideUp(200).slideDown(100); 

In the above statement, the color, slideup, and slidedown action are applied on the heading selector in the same line using chaining.

Q10. What is jQuery UI?

As the name suggests, jQuery UI is a bunch of GUI widgets, animation effects, and themes. It is implemented using jQuery, CSS, and HTML. It is an open-source JavaScript library that is first released by the jQuery Foundation in 2007 and is now under the MIT license. It is one of the popular JavaScript libraries that is used in some of the popular websites for designs and animations like Pinterest, PayPal, Netflix, and IMDb.

Q11. What is $() in jQuery?

The jQuery() can also be written as $(). It is used as a selector for searching through the DOM elements that match the provided selector. It searches within the DOM element at the document root.

Example

$(“h1”).hide() ;

In the above statement, $() selects the h1 heading and applies the provided action. It hides all the heading h1 tags in the document.

Q12. What are plugins in JQuery?

Plugin in jQuery is a file written in standard JavaScript code. This file contains some methods that can be used with jQuery methods. These plugins can be downloaded from the official repository of the jQuery plugins. The plugin can be included in the jQuery file similar to other libraries in the head of the document. You can even create your own plugin.

Example of a plugin

<head> 
    <script src = "jquery.plug-in.js" type = "text/javascript"></script> 
</head> 

Q13. How to disable animation in jQuery?

The jQuery stop() function is used to stop all the animations currently running on the selected elements. It stops the animation before it is finished.

Syntax

$(selector).stop(stopAll,goToEnd); 

To completely disable elements globally, then set the jQuery.fx.off property to true. The default value is false which lets run all the animations normally. Setting it true disables all animations from all the elements immediately and sets the element to the final state.

Syntax

jQuery.fx.off = true|false; 

Q14. When $(document).ready() function is used in Jquery?

The $(document).ready(function(){}) is executed first whenever the document is loaded. The ready event executes when the DOM (Document Object Model) is loaded. Whenever you need to run a function after the document is loaded, you can use this function.

Q15. Enlist few jquery methods used to provide effects?

Some of the jQuery methods to provide effects are:

  • animate() - It is used to run custom animation on the selected items.
  • delay() - It sets a delay time for the queued functions to work on the selected items.
  • show() - It is used to show the selected items.
  • stop() - It stops the current animation on the selected items.
  • hide() - It is used to hide the selected items.
  • fadeIn() - It fades in the selected items.
  • fadeOut() - It fades out the selected items.
  • toggle() - It toggles the animation between the hide() and show() methods on the selected items.

Q16. Enlist different slide effects available in Jquery?

There are three different sliding methods available in jQuery.

SlideDown() :This method is used to slideDown an element.

Syntax

$(selector).slideDown(speed,callback); 

Here the speed parameter specifies the duration of the slideDown effect such as “slow”, “fast", or in milliseconds. The callback function is optional and is executed after the animation slideDown is completed on the specified element.

SlideUp() : It is the opposite of the sideDown() method. It is used to slide up an element.

Syntax

$(selector).slideUp(speed,callback); 

The speed parameter specifies the duration and the optional callback function is executed after the animation is completed.

SlideToggle() :This method is used to toggle between the slideUp() and slideDown() method. If the element has been slid up, this method will slide them up and vice versa.

Syntax

$(selector).slideToggle(speed,callback); 

The speed parameter specifies the duration of the effect. The callback function is an optional function that is executed after the completion of the effect.

Q17. List different types of filters available in jquery?

For filtering, jQuery provides various functions such as filter(), first(), last(), eq(), slice(), has(), and not().

  • The filter() method takes an expression and a function as arguments. It filters the expression with the applied elements and returns only the element that matches the filter. The first() method returns the first element from the set of matched elements.
  • The opposite of the first() is the last() method. It returns the last element from the matched set of elements.
  • The eq() method is used to filter the set of matched elements and returns the element with the specified index number.
  • The has() method filters the set of matched elements and returns only the element that has the specified descendant element.
  • The not() method is used to filter the elements and return the elements that don’t match the specified condition.
  • The slice() method filters the element specified by the range of indices.

Q18. What is $.each() function in jQuery?

The $.each() method is used to iterate over all the DOM elements in the jQuery object and executes a function for each matched element. This method is used in manipulating multi-element DOM.

Example

const arr = [ 'one','two'] 
$.each(arr, function(index, value) { 
console.log(value); 
}); 

The above function prints the element of the array as mentioned in the function of the $.each method.

Q19. What is jQuery.holdReady() function?

The holdReady() function in jQuery is used to delay the ready event. If you want to load additional jQuery plugins before the execution of the DOM element even though they are ready, this method is used. It is called early in the document in the tag. Events that have already fired before calling this method will not be affected.

Example

$.holdReady( true );
$.getScript( "myplugin.js", function() {
  $.holdReady( false );
});

Q20. How to clone and object in jQuery?

The clone method in jQuery is used to make a copy of the selected elements. It copies the child nodes, text, and attributes of the selected element.

Syntax

$(selector).clone(true|false) 

The optional parameter true or false specifies whether to clone the event handler or not. This method returns the cloned values of the selected elements.

Q21. How to check data type of any variable in jQuery?

The jQuery.type() or $.type() function is used to find the datatype of a specified object.

Syntax

JQuery.type(object); 

It returns the type of the object that is passed.

Q22. Write simple code to disable Browser's Back button in jQuery?

The below jQuery code is used to disable the back button in the browser.

<script type="text/javascript"> 
$(document).ready(function() { 
     window.history.pushState(null, "", window.location.href);        
     window.onpopstate = function() { 
         window.history.pushState(null, "", window.location.href); 
     }; 
}); 
</script> 

Q23. What is event.PreventDefault in jQuery?

The event.preventDefault() method is used to stop all the default actions and event bubbling of an element from starting.

Syntax

event.preventDefault() 

&lt;

<script> 
$(document).ready(function(){ 
$("a").click(function(event){ 
   event.preventDefault(); 
}); 
}); 
</script> 

Here, the preventDefault() method prevents the link from opening the URL.

Q24. What are the different type of selectors in Jquery?

There are 3 types of selectors in Jquery

  • CSS Selector
  • XPath Selector
  • Custom Selector

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

Q26. Explain width() vs css(‘width’) in JQuery

In jQuery, there is 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.

Q27. Explain bind() vs live() vs delegate() methods in Jquery

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());
});
});

Q28. What is the use of param() method in JQuery .

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”

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

Q30. 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”);

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

Q32. What are the various ajax functions available in Jquery ?

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.

Q33. Explain .empty() vs .remove() vs .detach() in Jquery

  • .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();

Q34. 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);
});

Q35. How is body onload() in JQuery is different from document.ready() function?

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.

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