How do you use the addEventListener method in JavaScript?
addEventListener attaches event handlers to elements. See its syntax, options, event delegation, removal with AbortController and common mistakes.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
addEventListener attaches event handlers to elements. See its syntax, options, event delegation, removal with AbortController and common mistakes.
Functional programming in JavaScript composes pure functions and avoids mutation. See immutability, higher-order functions, currying and composition with examples.
How the JavaScript new keyword builds an object step by step, with classes, constructor functions, new.target and common mistakes.
The JavaScript event queue stores ready callbacks for the event loop. Learn how tasks and microtasks are ordered, with a worked example and output.
Promise.race returns a promise that settles with the first input promise to settle. See timeout examples, pitfalls and how it differs from Promise.any.
Learn when to use textContent, innerHTML, insertAdjacentHTML and DOM methods to change element content safely, with working examples.
Learn how JavaScript template literals work: backtick syntax, ${} interpolation, multi-line strings, tagged templates, String.raw and common pitfalls.
Event delegation handles clicks on many children with one parent listener using event bubbling. See closest(), dynamic elements and which events cannot be delegated.
Use event.stopPropagation() to stop an event bubbling to parent elements, and learn when event delegation is a cleaner alternative.
Event bubbling carries a DOM event from the clicked element up through its parents. See how it works, how to stop it and why delegation relies on it.
Use addEventListener to react to clicks, input and other events. Covers options, removal with AbortController, event delegation and accessible markup.
Call element.remove() to delete a node, use replaceChildren() to clear a container, and release listeners and references so removed elements are freed.
Use document.createElement, configure the node, then insert it with append, prepend or before. Covers fragments, templates, SVG and safe text handling.
getElementById looks up one element by id, while querySelector accepts any CSS selector. See when to use each, with examples and common pitfalls.
Use querySelector and querySelectorAll for CSS selectors and getElementById for ids. Learn the return types, live versus static results and common mistakes.
The Document Object Model is a live tree of nodes that JavaScript can read and modify. Learn how to select, update, create and listen to elements.
A mixin shares methods between unrelated JavaScript classes without a common parent. See Object.assign and subclass factory patterns, plus their pitfalls.
Instance methods act on one object through this; static methods belong to the class. See when to use each with modern ES2022 examples.
Use # private fields for real privacy in classes, or closures and module scope without classes. Symbol keys and underscores only hide data by convention.
Static methods belong to the class, not its instances. See the syntax, how this behaves, static fields, inheritance and common mistakes.