Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
// BAD: DOM queried 4 times for same element $("#nav").addClass("active"); $("#nav").css("top", "0"); $("#nav").show(); $("#nav").find("a").first().focus(); // GOOD: cached const $nav = $("#nav"); $nav.addClass("active").css("top", "0").show().find("a").first().focus(); // Cache selectors used in loops const $items = $(".item"); $items.each(function() { $(this).text($(this).data("value")); }); // still re-wraps $items.each(function(i, el) { el.textContent = el.dataset.value; }); // avoid wrapper
Result
Open