The Document Object Model (DOM) is a programming interface for web documents. It represents the page as a structured tree of objects that can be manipulated with JavaScript.
document.getElementById(id)document.getElementsByClassName(class)document.getElementsByTagName(tag)document.querySelector(selector)document.querySelectorAll(selector)node.innerHTML: Get/set HTML contentnode.textContent: Get/set text contentnode.innerText: Similar to textContent but respects CSS stylesnode.nodeType: Type of the node (1 = Element, 3 = Text, etc.)node.nodeName: Name of the node (e.g., DIV, SPAN)parentNode, childNodes, firstChild, lastChild
children, firstElementChild, lastElementChildnextSibling, previousSibling, nextElementSiblingdocument.createElement("tag")node.appendChild(child)node.insertBefore(newNode, referenceNode)node.replaceChild(newNode, oldNode)node.removeChild(child)element.setAttribute("attr", "value")element.getAttribute("attr")element.removeAttribute("attr")element.id, element.href, etc.element.classList.add("class")element.classList.remove("class")element.classList.toggle("class")element.classList.contains("class")element.style.property (e.g., element.style.color = "red")window.getComputedStyle(element)element.addEventListener("event", callback)element.removeEventListener("event", callback)click, mouseover, keydown, submit
Event Object Properties:
event.target, event.currentTargetevent.preventDefault(), event.stopPropagation()Attaching one event listener to a parent element to handle events from children using
event.target. Efficient for dynamically added elements.
document.forms["formName"]inputElement.valueelement.checkedform.submit()documentFragment when adding multiple elementsconst / let for variables
const btn = document.querySelector("#clickMe");
btn.addEventListener("click", () => {
const newDiv = document.createElement("div");
newDiv.textContent = "You clicked!";
document.body.appendChild(newDiv);
});
console.log() to inspect elements and eventsEvent listeners allow JavaScript to "listen" for interactions or changes in the DOM. They enable dynamic, interactive websites.
MDN Docs: addEventListener()
element.addEventListener("eventType", callbackFunction, useCapture);
eventType: A string like "click", "keydown", "submit"callbackFunction: Function to run when the event firesuseCapture (optional): Boolean for event phase (true = capture, false = bubbling)
document.querySelector("#btn").addEventListener("click", function() {
alert("Button Clicked!");
});
mouseover, mouseout, mousemove, mousedown,
mouseup, dblclick
element.addEventListener("mouseover", () => {
element.style.backgroundColor = "yellow";
});
keydown, keypress, keyup
document.addEventListener("keydown", function(e) {
console.log("Key pressed:", e.key);
});
submit, change, input, focus, blur
document.querySelector("form").addEventListener("submit", function(e) {
e.preventDefault(); // Prevents actual submission
alert("Form submitted!");
});
load, resize, scroll, beforeunload
window.addEventListener("resize", () =$gt; {
console.log("Window resized!");
});
touchstart, touchmove, touchend
const myEvent = new Event("customEvent");
element.addEventListener("customEvent", () =$gt; console.log("Custom event fired!"));
element.dispatchEvent(myEvent);
The callback function receives an event object with useful properties:
event.type โ Event nameevent.target โ The element that triggered the eventevent.currentTarget โ The element the listener is attached toevent.preventDefault() โ Stops default action (e.g., form submit)event.stopPropagation() โ Stops event bubblingEvents travel in three phases:
To handle capturing:
element.addEventListener("click", handler, true); // capture = true
function logClick() {
console.log("Clicked");
}
element.addEventListener("click", logClick);
element.removeEventListener("click", logClick);
Note: You must reference the same function to remove it!
Attach a single listener to a parent and use event.target to handle child interactions.
document.querySelector("#list").addEventListener("click", function(e) {
if (e.target.tagName === "LI") {
console.log("Clicked:", e.target.textContent);
}
});
โ Efficient for dynamic elements!
const for named event handler functionsThis is a paragraph. Click the button to change this text.
This box changes color when you click the button.
Click the button to see an alert.
Click the button to highlight all paragraphs.
Click the button to add or remove an item.
This text can be toggled.