jQuery
In short: A fast, small JavaScript library that simplifies HTML DOM traversal and manipulation, event handling, animation and AJAX with a concise, chainable API that worked consistently across browsers (especially old IE).
- It also had a large plugin ecosystem.
- Modern browsers now offer standard APIs (
querySelector,fetch,classList), so new projects often don't need it, but many existing apps still do.
$(document).ready() and window.onload?In short: $(document).ready() (or $(fn)) runs as soon as the DOM is parsed and ready to manipulate, before images and stylesheets finish loading;
- you can have many ready handlers.
window.onload(or$(window).on("load")) waits until all resources, including images and iframes, have loaded — later.- Use ready for DOM setup, load for things needing image dimensions.
$ sign in jQuery? What does noConflict do?In short: $ is an alias for the jQuery function, used to select elements, create elements and run code on DOM ready.
- Other libraries (old Prototype.js) also use
$; jQuery.noConflict()releases$back to them, and you can usejQuery(...)or wrap your code in(function ($) { ... })(jQuery)to keep using$locally.
.text(), .html() and .val()?In short: .text() gets/sets the text content (HTML-escaped, safe for user input).
.html()gets/sets inner HTML (parses markup — XSS risk with untrusted data)..val()gets/sets the value of form elements (input, select, textarea).
In short: Most jQuery methods return the same jQuery object, so you can call multiple methods in sequence on one selection: $("#box").addClass("active").fadeIn(200).text("Hello").
- It avoids repeated DOM queries and keeps code concise.
- Getter methods (like
.text()with no args) return values and end the chain.
.on(), .bind(), .live() and .delegate()?In short: .bind() attached handlers directly to existing elements;
.live()attached to the document for current and future elements (removed in 1.9, slow);.delegate()attached to a chosen ancestor for matching descendants..on()(1.7+) unified all of them:.on("click", handler)for direct binding and.on("click", ".child", handler)for delegation.- Use
.on()/.off()only.
In short: Binding a handler to a static ancestor with a selector argument — $("#table").on("click", ".delete", fn) — so events bubbling from matching descendants are handled.
- It works for elements added dynamically after binding (e.g. rows inserted via AJAX) and uses one handler instead of hundreds.
.attr() and .prop()?In short: .attr() works with HTML attributes as written in markup (initial values, strings).
.prop()works with DOM properties reflecting the current state (booleans likechecked,disabled,selected).- E.g. after a user ticks a checkbox,
.prop("checked")istruebut.attr("checked")may still be undefined. - Use
.propfor boolean state since jQuery 1.6.
In short: $.ajax({ url, method, data, contentType, dataType }) or shortcuts $.get, $.post, $.getJSON, .load().
- They return jqXHR objects implementing the Deferred/Promise interface: chain
.done(),.fail(),.always(),.then(), and combine with$.when()for parallel calls. - Since 3.0 deferreds are Promises/A+ compatible, so they can be awaited.
- For ASP.NET Core POSTs, send the antiforgery token in a header.
.remove(), .detach() and .empty()?In short: .remove() removes the elements and their jQuery data and event handlers.
.detach()removes elements but keeps data and handlers so they can be re-inserted later..empty()removes only the child nodes of the selected elements, keeping the elements themselves.
In short: Add a function to $.fn (jQuery.prototype), merge default options with $.extend, iterate with this.each() and return this so it's chainable, and wrap it in an IIFE receiving jQuery as $ to avoid conflicts: `(function($){ $.fn.myPlugin = function(opts){ const s = $.extend({}, defaults, opts);
- return this.each(function(){ /* ...
- */ }); }; })(jQuery);`.
In short: The unobtrusive adapter parses forms only on page load.
- After inserting new HTML, remove the cached validator data and re-parse: `$form.removeData("validator").removeData("unobtrusiveValidation");
- $.validator.unobtrusive.parse($form);`.
- Then call
$form.valid()before submitting.
In short: Usually not: modern browsers have standard equivalents (querySelector, classList, fetch, closest, dataset, CSS transitions), and frameworks provide components and state management. jQuery is still reasonable for small enhancements on server-rendered pages or when a needed plugin depends on it.
- To migrate: upgrade with jQuery Migrate, freeze new jQuery usage via lint rules, replace utilities with native APIs, swap plugins, migrate UI screens to components incrementally, then remove the library.
In short: The handlers were bound directly ($(".delete").on("click", ...)) to rows existing at bind time; new rows don't have them.
- Use delegation on a static container:
$("#ordersTable").on("click", ".delete", handler)— or rebind after each refresh (worse). - Make sure the container itself isn't replaced by the AJAX refresh.
In short: Upgrade to the latest jQuery 3.x/4.x (3.5+ fixed the htmlPrefilter XSS issues), using jQuery Migrate in development to find deprecated API usage and fix it (e.g. .size(), .bind(), .success() callbacks);
- update plugins to compatible versions;
- review code that passes user data into
.html(),.append()with strings or$(userInput)and switch to.text()/element creation; - add a Content-Security-Policy;
- retest the UI.
$("#tbody").append(...). How do you speed it up?In short: Each .append() triggers DOM work and possibly layout.
- Build all rows as one HTML string (with escaped values) or a DocumentFragment and append once;
- cache
$("#tbody"); - detach the table during the build;
- use event delegation instead of per-row handlers;
- better yet, paginate or virtualise the table (DataTables with server-side processing) instead of rendering 5,000 rows.