jQuery Interview Questions
Answers written to be said out loud in an interview — each with a common follow-up question and a link to the tutorial that explains it in depth.
- Beginner
- 9
- Intermediate
- 10
- Experienced
- 6
01What is jQuery and why was it popular?Beginner
A JavaScript library that simplified DOM selection, events, animations and AJAX with a concise, chainable API and smoothed over browser inconsistencies at a time when browsers differed greatly.
02What does $(document).ready() do?Beginner
It runs a function once the DOM is fully parsed, so elements can be safely selected. The shorthand is $(function () { … }). It does not wait for images like window.onload does.
03What is the difference between .text(), .html() and .val()?Beginner
.text() gets or sets text content (safe against HTML injection). .html() gets or sets inner HTML (unsafe with untrusted input). .val() gets or sets the value of form fields.
04What does the $ function do in jQuery?Beginner
$ is an alias for jQuery. Given a CSS selector it returns a jQuery object wrapping the matching elements; given HTML it creates elements; given a function it runs it when the DOM is ready; given a DOM element it wraps it.
05How do you include jQuery in a page?Beginner
Load it from a CDN or a local file with a <script> tag before your own scripts, or install it with npm and import it in a bundler. Use the minified production build and, from a CDN, add an integrity hash.
06What is implicit iteration?Beginner
Most jQuery methods act on every element in the set without a loop: $(".item").addClass("active") updates all matches. Getters such as .val() or .attr() read only the first element.
07What is method chaining?Beginner
Most jQuery setters return the same jQuery object, so calls can be chained: $("#box").addClass("on").fadeIn(200).text("Ready"). It keeps code short and selects the elements only once.
08How do you add and remove classes with jQuery?Beginner
addClass, removeClass and toggleClass, plus hasClass to check. Prefer toggling classes over setting inline styles with .css(), so presentation stays in the stylesheet.
09What is the difference between .attr() and .prop()?Beginner
.attr() reads the HTML attribute as written in the markup. .prop() reads the live DOM property. For checked, selected and disabled use .prop(), because the attribute does not change when the user clicks.
10How do you handle events for elements added dynamically in jQuery?Intermediate
Use delegated events: $(container).on("click", ".item", handler). The listener sits on an existing parent and handles clicks from current and future .item elements.
11How do you make an AJAX request with jQuery?Intermediate
$.ajax({ url, method, data, dataType }) or the shortcuts $.get, $.post and $.getJSON. They return a jqXHR promise-like object that supports .done(), .fail() and .always(), and can be awaited.
12What is the difference between .find() and .children()?Intermediate
.children() only looks one level down at direct children. .find() searches all descendants.
13append vs prepend vs after vs before?Intermediate
append and prepend insert content inside the element, at the end or start. after and before insert it outside, as a sibling. appendTo and prependTo do the same with the target and content swapped.
14How do you stop jQuery animations from queuing up?Intermediate
Call .stop() before starting a new animation — .stop(true, true) clears the queue and jumps to the end. Without it, hovering quickly in and out queues dozens of animations that keep running.
15closest() vs parents() vs parent()?Intermediate
parent() returns the direct parent. parents() returns all ancestors, optionally filtered. closest() starts with the element itself and walks up until the first match, so it returns zero or one element.
16How do you serialise a form with jQuery?Intermediate
.serialize() returns a URL-encoded string (name=Asha&email=a%40b.com) ready for $.post. .serializeArray() returns [{ name, value }] pairs. Only fields with a name attribute, that are enabled and successful, are included.
17What does $.ajax return, and how do you handle success and failure?Intermediate
A jqXHR object that implements the promise interface. Chain .done(), .fail() and .always(), or use .then(). In modern code you can also await it.
18How do you send an anti-forgery token with jQuery AJAX in ASP.NET Core?Intermediate
Render the token in the page (@Html.AntiForgeryToken() or a meta tag) and send it in the RequestVerificationToken header for every POST, for example with $.ajaxSetup({ headers: { … } }).
19How do you write a simple jQuery plugin?Intermediate
Add a function to $.fn, loop with this.each(), merge options with $.extend({}, defaults, options) and return this so the plugin stays chainable.
$.fn.highlight = function (options) {
const settings = $.extend({ color: 'yellow' }, options);
return this.each(function () {
$(this).css('background', settings.color);
});
};
$('.note').highlight({ color: '#fde68a' });
20How would you migrate a jQuery codebase to modern JavaScript?Experienced
Incrementally: map common calls to native APIs (querySelectorAll, classList, fetch, addEventListener), remove plugins one at a time, keep jQuery only where unobtrusive validation or plugins still need it, and add tests around the behaviour you change.
21What are common jQuery performance mistakes?Experienced
Repeating the same selector in a loop instead of caching it, building HTML with many .append() calls instead of one, binding a handler to every row instead of delegating, and using broad selectors like $(".x") without a context.
22How do you avoid XSS with jQuery?Experienced
Insert user data with .text() or by setting properties, not .html(), .append(string) or $(string). Never build selectors or HTML from user input, and keep jQuery updated — old versions had XSS issues in $() parsing.
23What is the most common bug when content is loaded with AJAX?Experienced
Handlers bound with $(".btn").on("click") at page load do not apply to elements added later. Use delegation on a stable parent: $("#list").on("click", ".btn", handler).
24Is jQuery still a good choice for a new project?Experienced
Rarely for a new single-page app: modern DOM APIs, fetch and frameworks cover what it offered. It is still reasonable for small enhancements on server-rendered pages, for legacy codebases and where a plugin you need depends on it.
25How would you validate a form on the client with jQuery in an MVC app?Experienced
Use jQuery Validation with the unobtrusive adapter: Data Annotations on the model render data-val-* attributes, and the scripts validate on blur and submit without writing JavaScript. The server must still validate.