Introduction to jQuery
You will meet jQuery in many existing projects. This lesson shows you how to read it confidently and when plain JavaScript is the better choice.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 1, 2026
Related tutorials
Learning objectives#
- Recognise the $() function and chaining
- Wait for the DOM with $(fn)
- Map common jQuery calls to modern DOM APIs
- Decide whether to keep or remove jQuery
Explanation#
jQuery wraps elements in a jQuery object. $(selector) finds elements; methods like .addClass() and .on() then act on every matched element at once, and most methods return the same object so calls can be chained.
jQuery was essential when browsers had inconsistent APIs. Today querySelectorAll, classList, addEventListener and fetch cover most of its features natively.
Keep jQuery in a project that already depends on it heavily (for example, plugins or ASP.NET MVC unobtrusive validation). Avoid adding it to new projects just for DOM selection.
Code example: jQuery and its modern equivalent#
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<button id="jq">jQuery toggle</button>
<button id="js">Vanilla JS toggle</button>
<p class="note" style="font-family:sans-serif">Toggle my highlight.</p>
<style>.hl { background: #fde68a; }</style>
<script>
// jQuery
$(function () {
$('#jq').on('click', function () {
$('.note').toggleClass('hl');
console.log('jQuery toggled');
});
});
// Modern JavaScript equivalent
document.querySelector('#js').addEventListener('click', () => {
document.querySelectorAll('.note').forEach(el => el.classList.toggle('hl'));
console.log('Vanilla JS toggled');
});
</script>
Both buttons toggle a yellow highlight on the paragraph and log a message.
Step-by-step explanation#
- $(function () { … }) runs the code once the DOM is ready.
- $("#jq").on("click", …) attaches a click handler.
- $(".note").toggleClass("hl") toggles the class on every matching element.
- The vanilla version uses querySelectorAll and classList.toggle for the same result with no library.
Real-world use case#
Maintaining an ASP.NET MVC 5 application that uses jQuery for AJAX and jquery.validate.unobtrusive for client-side validation.
Common mistakes#
$(".x")[0] is a DOM element, not a jQuery object, so jQuery methods are not available on it.
Cache selections in a variable: const $list = $("#list").
Best practices#
- Load jQuery once, from a pinned version
- Prefix variables that hold jQuery objects with $
- Use event delegation with .on(event, selector, handler)
- Prefer native APIs for new code
Remember this#
$(selector) finds; .method() acts on every match.
Most jQuery methods return the same object — chain them.
Existing jQuery project: keep it. New project: try native JS first.
Practice exercise#
Your task: Convert $(".item").addClass("active").text("Selected"); to modern JavaScript.
Need a hint?
Show solution
document.querySelectorAll('.item').forEach(el => {
el.classList.add('active');
el.textContent = 'Selected';
});
Mini quiz#
What is the modern equivalent of $(".card").addClass("open")?
Want more? Practise all jQuery MCQs · jQuery interview questions
My notes#
Saved in this browser.