jQuery interview question · Most asked
What are the native JavaScript equivalents of common jQuery operations?
Summary
$(sel) becomes document.querySelectorAll(sel); .addClass/.removeClass/.toggleClass become el.classList.add/remove/toggle; .on() becomes addEventListener (delegation via event.target.closest(sel)); .closest() is el.closest(); .text() / .html() are textContent / innerHTML; .attr() is getAttribute/setAttribute; .data() is el.dataset; $.ajax is fetch; and $(fn) is DOMContentLoaded or a defer script.
Longer Answer
document.querySelector("#orders").addEventListener("click", (e) => {
const btn = e.target.closest(".delete-order");
if (!btn) return;
e.preventDefault();
deleteOrder(btn.dataset.id);
});
Differences to remember: querySelectorAll returns a static NodeList (use forEach or spread it), native methods do not apply to a whole collection automatically, and fetch does not reject on HTTP errors, so check response.ok.
Key Points
- querySelectorAll, classList, closest, dataset.
- addEventListener + closest for delegation.
- fetch instead of $.ajax.
- Collections and fetch errors behave differently.
More most-asked jQuery questions
- How do you make AJAX requests with jQuery?
- What is the difference between .html() and .text(), and why does it matter for security?
- What is event delegation, and why is it useful?
- What is the difference between $(document).ready(), $(fn), DOMContentLoaded and window.onload?
- How do you bind and unbind events in modern jQuery? What happened to bind, live and delegate?
- What is the difference between .attr() and .prop()?
- What is the difference between preventDefault(), stopPropagation() and returning false in a jQuery handler?
- What is jQuery, and is it still relevant today?