This document covers many aspects of DOM manipulation and event handling including methods to get and set attributes, create and insert elements, and a vast range of events: mouse events, keyboard events, focus, drag and drop, touch, pointer, resize and more. Each section includes code examples for clarity.
Retrieves the value of a specified attribute on the element.
// Example: Getting the href attribute
const link = document.querySelector('a');
const hrefValue = link.getAttribute('href');
Sets the value of an attribute on the specified element.
// Example: Setting a custom data attribute
const div = document.createElement('div');
div.setAttribute('data-role', 'admin');
Creates a new HTML element of the given tag name.
// Creating a new paragraph element
const p = document.createElement('p');
p.innerText = 'Hello, world!';
Adds a node to the end of the list of children of a specified parent node.
// Appending the paragraph to a container element
const container = document.getElementById('container');
container.appendChild(p);
querySelector() — Selects the first element that matches a specified CSS selector.querySelectorAll() — Selects all elements that match a specified CSS selector.remove() — Removes the element from the DOM.replaceChild() — Replaces a child node with a new node.cloneNode() — Creates a copy of the node.// Example: Removing an element
const element = document.querySelector('.remove-me');
element.remove();
element.addEventListener("click", () => {
console.log("Element clicked!");
});
element.addEventListener("dblclick", () => {
console.log("Element double-clicked!");
});
element.addEventListener("mouseenter", () => {
console.log("Mouse entered!");
});
element.addEventListener("mouseleave", () => {
console.log("Mouse left!");
});
element.addEventListener("mousemove", (e) => {
console.log("Mouse position:", e.clientX, e.clientY);
});
element.addEventListener("wheel", (e) => {
console.log("Mouse wheel used", e.deltaY);
});
document.addEventListener("keydown", (e) => {
console.log("Key down:", e.key);
});
document.addEventListener("keypress", (e) => {
console.log("Key pressed:", e.key);
});
document.addEventListener("keyup", (e) => {
console.log("Key up:", e.key);
});
input.addEventListener("focus", () => {
console.log("Input focused!");
});
input.addEventListener("blur", () => {
console.log("Input lost focus!");
});
window.addEventListener("scroll", () => {
console.log("Scrolled to:", window.scrollY);
});
window.addEventListener("resize", () => {
console.log("Window size changed:", window.innerWidth, window.innerHeight);
});
element.addEventListener("dragstart", (e) => {
e.dataTransfer.setData("text/plain", e.target.id);
console.log("Drag started");
});
dropZone.addEventListener("dragover", (e) => {
e.preventDefault();
console.log("Drag over");
});
dropZone.addEventListener("drop", (e) => {
e.preventDefault();
const data = e.dataTransfer.getData("text/plain");
console.log("Dropped element id:", data);
});
element.addEventListener("touchstart", (e) => {
console.log("Touch started");
});
element.addEventListener("touchmove", (e) => {
console.log("Touch moving");
});
element.addEventListener("pointerdown", (e) => {
console.log("Pointer down event", e.pointerType);
});
element.addEventListener("pointerup", (e) => {
console.log("Pointer up event");
});
element.addEventListener("pointermove", (e) => {
console.log("Pointer moved:", e.clientX, e.clientY);
});
element.addEventListener("pointerenter", () => {
console.log("Pointer entered element");
});
element.addEventListener("pointerleave", () => {
console.log("Pointer left element");
});
element.addEventListener("transitionend", () => {
console.log("Transition completed");
});
element.addEventListener("animationstart", () => {
console.log("Animation started");
});
element.addEventListener("animationend", () => {
console.log("Animation ended");
});
element.addEventListener("animationiteration", () => {
console.log("Animation iteration");
});
Methods to add, remove or toggle classes on an element.
element.classList.add("new-class");
element.classList.remove("old-class");
element.classList.toggle("active-class");
Accessing parent nodes, child nodes, firstChild, lastChild, etc.
const parent = element.parentNode;
const children = element.childNodes; // includes text nodes
Use addEventListener options to set event propagation. Use e.stopPropagation() to cancel bubbling.
parent.addEventListener("click", () => {
console.log("Parent handler");
}, true); // capturing phase
Monitor changes to the DOM tree.
const observer = new MutationObserver((mutations) => {
mutations.forEach(mutation => console.log(mutation));
});
observer.observe(document.body, { childList: true, subtree: true });
This document is designed as a quick reference for many common DOM methods and events. While it is comprehensive, keep in mind that the DOM is vast and many libraries and frameworks may extend these concepts further.
For more interactive examples, consider integrating this documentation with a live code editor or linking to a comprehensive guide on MDN.