Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Monday, September 11, 2023

Top 20 jQuery Interview Questions and Answers for 1 to 3 Years Experienced

Without a doubt, jQuery has given a much-needed boost to JavaScript, a language so useful but equally underrated at times. Before jQuery comes into the picture, we used to write lengthy JavaScript code not just for bigger but even for smaller functions. Those codes were at times both difficult to read and maintain. Having written JavaScript before using this excellent library, I realized the true power of jQuery, just after using it for a month. Given its huge popularity, jQuery interview questions are increasingly asked in any web developer interview, not just beginners but also experienced developers, including HTML and JavaScript.

Since jQuery is relatively new, most interview questions are asked from the core jQuery library including selectors, DOM manipulation, and jQuery basics.

In this article, I am sharing a list of 16 jQuery questions asked to HTML and JavaScript programmers in different interviews. Some of these questions are also asked in the Java Web development interview, where it's required to work on both server-side (Spring, Servlet and JSP) and client-side (HTML, CSS, JavaScript, and jQuery).

If you are going for an interview, where the role demands multiple skills e.g. Java, jQuery, it's not expected from you to know every minor detail or comprehensive knowledge of jQuery, but if you are going for a pure client-side development role, you might get more tricky and advanced jQuery questions than mentioned in this article.

Nevertheless, you can use it to quickly revise some of the most frequently asked jQuery questions on interviews, but they are most suited for web developers with 2 to 5 years of experience, especially in the Java stack.

By the way, if you are seriously preparing for a jQuery interview and looking for some more questions for the telephonic and face-to-face round, then I suggest taking a look at this nice little book, jQuery Interview Questions You'll Most Likely Be Asked. It contains both basic and advanced questions to give you an idea of what kind of questions you can expect in real interviews.

20 jQuery Interview Questions and Answers for 1 to 2 years Experienced

JavaScript is a standard for client-side scripting and jQuery makes writing JavaScript much easier. You can achieve a lot more by just writing a couple of lines of jQuery code. It is one of the most used JavaScript libraries and there is hardly any new project, which is using plain JavaScript without jQuery.

What this means to you, a Java web developer is that you will be bound to see a couple of jQuery interview questions in Java web development interviews. Earlier it was mostly HTTP, HTML, CSS, and JavaScript but now days apart from JavaScript fundamentals, people like to know whether you are familiar with jQuery or not.

This list of 16 jQuery questions is prepared for web developers and can be very handy to revise key concepts before telephonic or screening rounds of interviews. If you are new to jQuery then it will also help you to understand fundamentals better and inspire you to explore more.

1. What is $() in the jQuery library? (answer) 
The $() function is an alias of jQuery() function, at first it looks weird and makes jQuery code cryptic, but once you get used to it, you will love its brevity. $() function is used to wrap any object into jQuery object, which then allows you to call the various method defined jQuery object. You can even pass a selector string to the $() function, and it will return a jQuery object containing an array of all matched DOM elements. I have seen this jQuery asked several times, despite it's quite basic, it is used to differentiate between the developer who knows jQuery or not.

2. You have five <div> elements on your page? How do you select them using jQuery? (answer) Another fundamental jQuery question based on the selector. jQuery supports different kinds of selectors e.g. ID selector, class selector, and tag selector. Since in this question nothing has been mentioned about ID and class, you can use the tag selector to select all div elements.  jQuery code : $("div"), will return a jQuery object contain all five div tags.  For a more detailed answer, see the article.

3. Difference between ID selector and class selector in jQuery? (answer
If you have used CSS, then you might know the difference between ID and class selector, It's same with jQuery. ID selector uses ID e.g. #element1 to select element, while class selector uses CSS class to select elements. 

When you just need to select only one element, use ID selector, while if you want to select a group of elements, having same CSS class than use class selector. There is a good chance that Interview will ask you to write code using  ID and class selector. Following jQuery code uses ID and class selectors :

$("#LoginTextBox")  -- Returns element wrapped as jQuery object 
        with id="LoginTextBox"
$(".active") -- Returns all elements with CSS class active.

From a syntax perspective, as you can see, another difference between ID and class selector is that the former uses "#" and later uses the "." character. For more detailed analysis and discussion, see the answer.

4. How do you hide an image on a button click using jQuery? (answer) 
This jQuery interview question is based on event handling. jQuery provides good support for handling events like button click. You can use the following code to hide an image, found using Id or class. What you need to know is hide() method and how to setup an even handler for button, to handle clicks, you can use following jQuery code to do that :


I like this jQuery question because it's like a practical task and also code is not difficult to write.

5. What is $(document).ready() function? Why should you use it? (answer
This is one of the most important and frequently asked jQuery Interview question. ready() function is used to execute code when document is ready for manipulation. jQuery allows you to execute code, when DOM is fully loaded i.e. HTML has been parsed and DOM tree has been constructed. 

Main benefit of $(document).ready() function is that, it works in all browser, jQuery handles cross browser difficulties for you. For curious reader see answer link for more detailed discussion. You can also see first chapter of Head First jQuery to learn more about this key function.

6. Difference between JavaScript window.onload event and jQuery ready function?(answer
This is the follow-up of previous jQuery interview question. Main difference between JavaScript onload event and jQuery ready function is that former not only waits for DOM to be created but also waits until all external resources are fully loaded including heavy images, audios and videos.  

If loading images and media content takes lot of time that user can experience significant delay on execution of code defined in window.onload event. On the other hand jQuery ready() function only wait for DOM tree, and does not wait for images or external resource loading, which means faster execution. 

Another advantage of using jQuery $(document).ready() is that you can use it multiple times in your page, and browser will execute them in the order they appear in the HTML page, as opposed to onload technique, which can only be used for a single function. Given this benefits, it's always better to use jQuery ready() function than JavaScript window.onload event.  See answer article for more deep discussion.

7. How do you find all selected options of HTML select tag? (answer) 
This is one of the tricky jQuery question on Interviews. It's still a basic, but don't expect every jQuery beginners to know about this. You can use following jQuery selector to retrieve all selected options of <select> tag with multiple=true :

$('[name=NameOfSelectedTag] :selected')

This code uses attribute selector in combination of :selected selector, which returns only selected options. You can tweak this and instead of name, you can even use id attribute to retrieve <select> tag.

8. What is each() function in jQuery? How do you use it? (answer) 
each() function is like Iterator in Java, it allows you to iterate over a set of elements. You can pass a function to each() method, which will be executed for each element from the jQuery object, on which it has been called. 

This question sometime asked as follow-up of previous question e.g. how to show all selected options in alert box. We can use above selector code to find all selected option and than further can use each() method to print them in alert box, one by one, as shown below:

$('[name=NameOfSelectedTag] :selected').each(function(selected){

text() method returns text for that option.

9. How do you add an HTML element in DOM tree? (answer) 
You can use jQuery method appendTo() to add an HTML element in DOM tree. This is one of the many DOM manipulation method jQuery provides. You can add an existing element or a new HTML element, appendTo() add that method in the end of a particular DOM element.

10. Can you write jQuery code to select all links, which is inside paragraphs? (answer) 
Another jQuery interview question based on selector. This also required to write jQuery one liner, like many other questions. you can use following jQuery snippet to select all links (<a> tag) nested inside paragraphs (<p> tag).

11. Difference between $(this) and this keyword in jQuery? (answer) 
Could be a tricky questions for many jQuery beginners, but indeed it's simplest one. $(this) returns a jQuery object, on which you can call several jQuery methods e.g. text() to retrieve text, val() to retrieve value etc, while this represent current element, and it's one of the JavaScript keyword to denote current DOM element in a context. You can not call jQuery method on this, until it's wrapped using $() function i.e. $(this).

12. How do you retrieve attribute of an HTML tag using jQuery like href of links? (answer)
attr() method is used to retrieve value of an attribute of any HTML element. You first need to select all links or specified links using jQuery selector and than you can apply attr() method to get value of there href attribute. Below code will find all links from a page and return href value :


13. How do you set attributes using jQuery? (answer) 
One more follow-up question of the previous jQuery question, attr() method is overload like many other methods in JQuery. If you call attr() method with value e.g. attr(name, value), where name is the name of attribute and value is the new value.

14. What is difference between detach() and remove() method in jQuery? (answer) 
Though both detach() and remove() method is used to remove a DOM element,Main difference between them is that detach() keep track of the last element detached, so that it can be reattached, while remove() method does keep reference of last removed method. This is one of the many jQuery interview question from DOM manipulation. You can also take a look on appendTo() for adding element into DOM.

15. How do you add and remove CSS classes to an element using jQuery? (answer) 
By using addClass() and removeClass() jQuery methods. This can be very handy, while dynamically changing the class of elements e.g. marking them inactive or active and using class ".active" etc.

16. What is main advantage of loading jQuery library using CDN? (answer)
This is slightly advanced jQuery question, and don't expect that jQuery beginners can answer that. Well, apart from many advantages including reducing server bandwidth and faster download, one of the most important is that, if browser has already downloaded same jQuery version from same CDN, than it won't download it again. Since now days, almost many public websites use jQuery for user interaction and animation, there is very good chance that browser already have jQuery library downloaded. Curious reader, please see the answer for in depth analysis.

17. What is difference between jQuery.get() and jQuery.ajax() method?
ajax() method is more powerful and configurable, allows you to specify how long to wait and how to handle error, get() is a specialization to over ajax just to retrieve some data.

18. What is method chaining in jQuery? what is benefit of using method chaining?
Method chaining is calling another method in result of another method, it result in clean and concise code, single search over DOM so better performance.

19. What happen if you return false from a jQuery event handler? 
It used to stop the event bubbling up

20. Which one is more efficient, document.getElementbyId( "myId") or $("#myId)? 
First one because its direct call to JavaScript engine

Good jQuery Interview Questions with Answers

That's all on this list of 20 jQuery interview questions and answers for beginners. As I said already, these questions are pretty basic, but most likely asked web developers including Java. For an Interviewer, you can use this question to check if the candidate has really used jQuery or not. Since an Interview for a client-side position almost always contains loads of questions from HTML, CSS, and JavaScript, you got to prepare that as well, along with jQuery questions. 

By the way, if you have faced any other question on jQuery interviews and wants to share with us, you can do so by commenting in this article. You can also ask any jQuery question, asked to you on interviews.

BTW, If you love jQuery like me and wants to know more about it, you will enjoy these amazing articles :
  • Top 5 jQuery Books Every Web Developer Should Read? (see here)
  • How to get the current URL Parameter using jQuery? (solution)
  • How to use multiple jQuery Date picker elements in one HTML page? (answer)
  • How to create Tabbed UI using jQuery and HTML? (solution)
  • How to redirect web page using jQuery code? (answer)
  • 3 Ways to parse HTML using JSoup in Java? (solution)
  • What is the difference between SubStr and SubString method in JavaScript? (answer)
  • How to prevent double submission of POST data using JavaScript? (solution)
  • What is difference between == and === in JavaScript? (answer)


Anonymous said...

What is difference between jQuery.get() and jQuery.ajax() method? (ajax is more powerful and configurable, allows you to specify how long to wait and how to handle error, get() is a speciallization to over ajax just to retrieve some data)

What is method chaining in jQuery? what is benefit of using method chaining ( clean and concise code, single search over DOM so better performance)

what happen if you return false from a jQueyr event handler? (it used to stop the event bubbling up)

which one is more efficient, document.getElementbyId( "myId") or $("#myId)? first one because its direct call to JavaScript engine

Christopher said...

This article feels somewhat old, like from 2009.

It refers to jQuery as "relatively new", when actually was created 8 years ago. I don't see jQuery interview questions being "increasingly asked", instead they are decreasingly asked and being replaced by questions about other libraries and frameworks, like Angular, React, etc.

We are getting to the point when is starting to be considered a good practice to avoid using jQuery, and using just using plain js instead, thanks to the new features of vanilla JavaScript, like document.querySelectorAll. (and that feature is actually not that new)

The thing that I felt more dated was that mention of window.onload. I haven't seen window.onload being used since a long time ago. The modern way to attach an event is using addEventListener, and if you want to execute your function once the DOM is loaded instead of the entire page, just like ready(), you should attach your function to the "DOMContentLoaded" event (instead of the "load" event).

Anonymous said...

Can you please share some advanced jQuery questions for phone interviews? how about mix of JavaScript and jQuery telphonic questions?

Unknown said...

Uncaught ReferenceError: $ is not defined
at home:2625
1539976252.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
1539976622.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
users:1 Failed to load resource: the server responded with a status of 404 (Not Found)
add_field.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
1548444573.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
1548241232.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
1548448476.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
1553508647.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
1551678425.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
1571921049.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
why i get this type of error

Post a Comment