Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Can you tell the difference between prop() and attr()s?

Answer»

  • Both prop() and attr() can be used to get or set the value of the specified property of an element attribute.

  • The attr() gives the default value of a property whereas prop() returns its current value.

Conclusion

A JavaScript library called jQuery makes it simple to construct dynamic web pages. It contributes to the efficiency and interactivity of web applications. It also facilitates the usage of a number of functions that may be used to alter data, which speeds up the development process. JQuery has some important uses like - DOM manipulations, Availability of different plugins according to the work, Animation and AJAX Support, Support for Cross browsers, etc. jQuery is a lightweight library that can be easily integrated into any website. We have seen all the most frequently asked questions related to jquery in the interview.

Additional Interview Resources

  • https://www.interviewbit.com/html-interview-questions/

  • https://www.interviewbit.com/javascript-interview-questions/

  • https://www.interviewbit.com/technical-interview-questions/

  • https://www.interviewbit.com/blog/top-javascript-libraries/

  • https://www.interviewbit.com/blog/best-front-end-frameworks/

  • https://www.interviewbit.com/blog/javascript-projects/

  • https://www.interviewbit.com/javascript-mcq/

  • https://www.interviewbit.com/blog/javascript-vs-jquery/


2.

Can you provide an example of chaining using a code snippet?

Answer»

Old Code:

$(document).ready(function(){
$('#id').addClass('ib);
$('#id').css('color', 'blue);
$('#id').fadeIn('slow');
});

New Code after Chaining:

$(document).ready(function(){
$('#id').addClass('ib)
.css('color', 'blue')
.fadeIn('slow);
});
3.

Consider the below code snippet and assume that there are 5 elements on the page. What is the difference between the start and end times displayed?

Answer»
function getMinsSeconds() {
var dt = new Date();
return dt.getMinutes()+":"+dt.getSeconds();
}

$( "input" ).on( "click", function() {
$( "p" ).append( "Start: " + getMinsSeconds() + "<br />" );
$( "div" ).each(function( i ) {
$( this ).fadeOut( 1000 * ( i * 2 ) );
});
$( "div" ).promise().done(function() {
$( "p" ).append( "End: " + getMinsSeconds() + "<br />" );
});
});

  • For the above code, the difference between the start and end times will be 10 seconds.

  • This is because .promise() will wait for all <div> animations (here, all fadeOut() calls) to complete, the last one will complete 10 seconds (i.e. 5 * 2 = 10 seconds) after the fadeOut() starts.


4.

Write A Jquery Code Snippet To Sort A String Array?

Answer»
$(document).ready(function(){
var arr = [ "jQuery","Interview","Questions","By","InterviewBit"];
sortedArr = arr.sort();
$("#elementId").html(sortedArr.join(""));
});

The Output will be

["By","InterviewBit","Interview","jQuery","Questions"]


5.

Explain the .promise() method in jQuery.

Answer»

  • The .promise() method returns a dynamically generated promise that is resolved when all actions of a certain type bound to the collection, queued or not, have ended.

  • The method takes two optional arguments:


    • type - The default type is “fx” which indicates that the returned promise is resolved only when all animations of the selected elements have been completed.


    • target - If a target object is specified, .promise() will attach to promise to that specified object and then return it rather than creating a new one.




6.

Can you write a jQuery code selector that needs to be used for querying all elements whose ID ends with string “IB”?

Answer»

We can use the following selector:

 $("[id$='IB']")


7.

How do you disable elements in Jquery Using "attr"?

Answer»
$('class_or_id_of_element_to_disable').attr('disabled', true);
$('class_or_id_of_element_to_disable').attr('disabled', false);
8.

Which of the two lines of code below is more efficient and why?

Answer»

document.getElementById("interviewBit"); OR $("#interviewBit");

  • The code document.getElementById( "interviewBit" ); is more efficient because its the pure JavaScript version.

  • The reason being jQuery is built on top of JavaScript and internally uses its methods to make DOM manipulation easier. This introduces some performance overhead. We need to remember that jQuery is not always better than pure JavaScript and we need to use it only if it adds advantage to our project.


9.

Write a code snippet for preventing the default behavior of the submit button for performing another event.

Answer»
<script>
$(document).ready(function(){
$("#submitButton").click(function(event){
event.preventDefault();
});
});
</script>
10.

Can you explain the difference between jQuery.get() and jQuery.ajax()?

Answer»


  • jQuery.ajax() allows the creation of highly-customized AJAX requests, with options for how long to wait for a response, what to do once the request is successful, how to handle a failure scenarios, whether the request to be sent is blocking (synchronous) or non-blocking (asynchronous), what format to expect as the response, and many more customizable options.


  • jQuery.get() is uses jQuery.ajax() underneath to create an AJAX request typically meant for simple retrieval of information.
    • There are various other pre-built AJAX requests given by jQuery such as:


      • jQuery.post() for performing post requests


      • jQuery.getScript() meant for loading and then executing a JavaScript file from the server using GET request.


      • jQuery.getJSON() for loading JSON-encoded data from the server using a GET HTTP request.





11.

Consider the following code that exists in following HTML with the CSS:

Answer»
<div id="expand"></div>

<style>
div#expand{
width: 50px;
height: 50px;
background-color: gray;
}
</style> Write jQuery code to animate the #expand div, expanding it from 50 * 50 pixels to 300 * 300 pixels within five seconds.

We can do this by using the animate() function. We first need to have access to the div element which has id value of expand and then apply animate function on the element as follows:

$("#expand").animate(
{
width: "300px",
height: "300px",
},
5000
);
12.

Write a jQuery code to create and delete cookies from the browser.

Answer»

There is no direct way to access the cookies using jquery. We can easily do this with the help of pure javascript. To work with cookies in jQuery, you must install the Dough cookie plugin or any other cookies plugins. The dough is simple to use and offers a number of useful capabilities.



  • Creating a cookies - $.dough("cookieName", "cookieValue");


  • Reading a cookies - $.dough("cookieName");


  • Deleting a cookies - $.dough("cookieName", "remove");


13.

What does the following code do?

Answer»
$( "div#firstDiv, div.firstDiv, ol#items > [name$='firstDiv']" )

  • The given code is an example of getting elements that satisfy multiple selectors at once. The function returns a jQuery object having the results of the query.

  • The given code does a query to retrieve those <div> element with the id value firstDiv along with all <div> elements that has the class value firstDiv and all elements that are children of the <ol id="items"> element and whose name attribute ends with the string firstDiv.


14.

How to perform jQuery AJAX requests?

Answer»

  • jQuery provides the ajax() method to perform an AJAX (asynchronous HTTP) request.

  • Syntax: $.ajax({name:value, name:value, ... }). The parameters specify one or more values of name-value pairs.


    • url: this name specifies the URL to send the request to. Default is the current page.


    • success(result,status,xhr): success callback function which runs when the request succeeds


    • error(xhr,status,error): A function to run if the request fails.


    • async: Boolean value that indicates whether the request should be handled asynchronous or not. The default value is true.


    • complete(xhr,status): A function to run when the request is completed (after success and error functions are handled)


    • xhr: A function used for creating the XMLHttpRequest object



  • Example:

$.ajax({
url: "resourceURL",
async: false,
success: function(result){
$("div").html(result);
},
error: function(xhr){
alert("An error occured: " + xhr.status + " " + xhr.statusText);
}
});