Modern JavaScript can do most jQuery tasks. Knowing both helps you read old code and write new code.
| Task | jQuery | Vanilla JavaScript |
|---|---|---|
| Select | $('.card') |
document.querySelectorAll('.card') |
| Ready | $(fn) |
document.addEventListener('DOMContentLoaded', fn) |
| Click | $('#b').on('click', fn) |
b.addEventListener('click', fn) |
| Text | $('#t').text('Hi') |
t.textContent = 'Hi' |
| Class | $('#t').addClass('on') |
t.classList.add('on') |
| Toggle | $('#t').toggleClass('on') |
t.classList.toggle('on') |
| Hide | $('#t').hide() |
t.hidden = true |
| Attribute | $('a').attr('href') |
a.getAttribute('href') |
| AJAX | $.getJSON(url) |
fetch(url).then(r => r.json()) |
When jQuery still makes sense
- The project already uses it (WordPress, older Laravel or PHP apps)
- You rely on jQuery plugins like DataTables or Select2
- Quick effects like
slideToggle()with one line
When to skip it
- New projects with modern browsers
- React, Vue or Angular apps (they manage the DOM themselves)
- Performance-critical pages where every KB matters (jQuery is about 30 KB gzipped)
Example
Example — Same feature, two ways
HTML
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>.on{background:#1f386b;color:#fff}</style>
<button id="jq">jQuery button</button>
<button id="js">JavaScript button</button>
<p id="out"></p>
<script>
// jQuery version
$(function () {
$('#jq').on('click', function () {
$(this).toggleClass('on');
$('#out').text('Toggled with jQuery');
});
});
// Vanilla JavaScript version
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('js').addEventListener('click', function () {
this.classList.toggle('on');
document.getElementById('out').textContent = 'Toggled with vanilla JavaScript';
});
});
</script>