THE REFERENCE ROOM

A little jQuery.

jQuery is a JavaScript library for selecting elements, handling events and updating the interface. Modern JavaScript can do many of the same jobs; this site uses jQuery for its navigation.

Selecting and clicking

Selecting and clicking

$(selector) selects elements; .on() attaches an event listener.

<button type="button" id="trigger">Try it</button>
<p id="message">Down the Rabbit-Hole</p>
Edit HTML
.highlight { background: #7FFF00; padding: 20px; }
.enter { animation: enter .8s ease-out; }
@keyframes enter {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .enter { animation: none; }
}
Edit CSS
$("#trigger").on("click", function () {
  $("#message").text("Hello from jQuery!");
});
Edit JAVASCRIPT

Try this → Compare this with the vanilla JavaScript examples.

Classes

Classes

addClass, removeClass and toggleClass mirror common classList actions.

<button type="button" id="trigger">Try it</button>
<p id="message">Down the Rabbit-Hole</p>
Edit HTML
.highlight { background: #7FFF00; padding: 20px; }
.enter { animation: enter .8s ease-out; }
@keyframes enter {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .enter { animation: none; }
}
Edit CSS
$("#message").addClass("highlight");
$("#trigger").on("click", function () {
  $("#message").toggleClass("highlight");
});
Edit JAVASCRIPT

Try this → Compare this with the vanilla JavaScript examples.

Show and hide

Show and hide

toggle() switches visibility. Use a button and expose the state for assistive technology.

<button type="button" id="trigger">Try it</button>
<p id="message">Down the Rabbit-Hole</p>
Edit HTML
.highlight { background: #7FFF00; padding: 20px; }
.enter { animation: enter .8s ease-out; }
@keyframes enter {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .enter { animation: none; }
}
Edit CSS
$("#trigger").attr("aria-expanded", "true").attr("aria-controls", "message");
$("#trigger").on("click", function () {
  $("#message").toggle();
  $(this).attr("aria-expanded", String($("#message").is(":visible")));
});
Edit JAVASCRIPT

Try this → Compare this with the vanilla JavaScript examples.

Menu transition

Menu transition

stop(true, true) clears queued effects. Reduced-motion preferences skip the sliding transition.

<button type="button" id="trigger">Try it</button>
<p id="message">Down the Rabbit-Hole</p>
Edit HTML
.highlight { background: #7FFF00; padding: 20px; }
.enter { animation: enter .8s ease-out; }
@keyframes enter {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .enter { animation: none; }
}
Edit CSS
$("#trigger").attr("aria-expanded", "true").attr("aria-controls", "message");
$("#trigger").on("click", function () {
  const open = $(this).attr("aria-expanded") === "true";
  const duration = matchMedia("(prefers-reduced-motion: reduce)").matches ? 0 : 180;
  $(this).attr("aria-expanded", String(!open));
  $("#message").stop(true, true).slideToggle(duration);
});
Edit JAVASCRIPT

Try this → Compare this with the vanilla JavaScript examples.

Without a library

The vanilla equivalent

The same text-changing interaction needs only standard browser APIs.

<button type="button" id="trigger">Try it</button>
<p id="message">Down the Rabbit-Hole</p>
Edit HTML
.highlight { background: #7FFF00; padding: 20px; }
.enter { animation: enter .8s ease-out; }
@keyframes enter {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .enter { animation: none; }
}
Edit CSS
const button = document.querySelector("#trigger");
const message = document.querySelector("#message");
button.addEventListener("click", () => {
  message.textContent = "Curiouser and curiouser!";
});
Edit JAVASCRIPT

Try this → Compare querySelector with $(), and textContent with .text().