Last updated: 2026-05-05

DOM manipulation

DOM manipulation refers to the process of using JavaScript to interact with and modify the Document Object Model (DOM), which represents the structure of a web page.

This is achieved using a series of DOM properties that are used to select, interact, listen to events, and alter parts of the webpage.


Element Selection

Property / MethodDescription
getElementById()Selects a single element by its id attribute. Returns the element or null if not found.
getElementsByClassName()Selects all elements with a given class name. Returns a live HTMLCollection.
getElementsByTagName()Selects all elements with a given tag name (e.g. "div", "p"). Returns a live HTMLCollection.
querySelector()Selects the first element that matches a CSS selector.
querySelectorAll()Selects all elements that match a CSS selector. Returns a static NodeList.

Content Manipulation

PropertyDescription
innerTextGets or sets the visible, rendered text of an element. CSS-aware — only returns what is actually visible on screen.
textContentGets or sets all text content inside an element, including text inside hidden elements. Faster than innerText and the safer choice for plain text.
innerHTMLGets or sets the HTML markup inside an element. ⚠️ Never use with untrusted user input — it opens the door to XSS (Cross-Site Scripting) attacks. Use textContent for plain text instead.
outerHTMLGets or sets the HTML of the element itself, including its own opening and closing tags.
nodeValueGets or sets the value of a text node or comment node directly.

Attribute Handling

Property / MethodDescription
idGets or sets the id attribute of an element.
classNameGets or sets the full class attribute of an element as a string.
getAttribute()Returns the value of a specified attribute on an element.
setAttribute()Sets the value of a specified attribute on an element. Creates the attribute if it doesn't exist.
removeAttribute()Removes a specified attribute from an element entirely.
hasAttribute()Returns true if the element has the specified attribute, false otherwise.

Style and Appearance

Property / MethodDescription
styleGets or sets inline CSS styles directly on an element (e.g. element.style.color = "red").
classListReturns the list of CSS classes on an element. Provides the following methods:
classList.add()Adds one or more class names to the element.
classList.remove()Removes one or more class names from the element.
classList.toggle()Adds the class if it is absent; removes it if it is present.
classList.contains()Returns true if the element has the specified class, false otherwise.

Element Creation and Removal

MethodDescription
createElement()Creates a new HTML element of the specified tag type (e.g. "div", "p").
appendChild()Appends a node as the last child of a specified parent element.
removeChild()Removes a specified child node from its parent element.
replaceChild()Replaces an existing child node with a new node.
createTextNode()Creates a new text node with the specified string content.
cloneNode()Creates a copy of an element. Pass true to also clone all its child elements.

Traversing the DOM

PropertyDescription
parentNodeReturns the parent node of the specified element.
childNodesReturns a NodeList of all child nodes, including text nodes and comments.
childrenReturns an HTMLCollection of only the element children, excluding text and comment nodes.
firstChildReturns the first child node, including text nodes.
firstElementChildReturns the first child that is an element, ignoring text and comment nodes.
lastChildReturns the last child node, including text nodes.
lastElementChildReturns the last child that is an element, ignoring text and comment nodes.
nextSiblingReturns the next node at the same level, including text nodes.
nextElementSiblingReturns the next element at the same level, ignoring text and comment nodes.
previousSiblingReturns the previous node at the same level, including text nodes.
previousElementSiblingReturns the previous element at the same level, ignoring text and comment nodes.

DOM Events

DOM events are actions or occurrences that happen in the browser — like a user clicking a button or typing in a field. To respond to these events, JavaScript uses addEventListener(), which attaches a function to an element that runs when the specified event occurs.

Syntax:

element.addEventListener('event', callbackFunction);

Example:

const button = document.querySelector('#submit-btn');

button.addEventListener('click', function() {
  console.log('Button was clicked!');
});
EventDescription
clickFires when an element is clicked.
inputFires when the value of an input field changes as the user types.
submitFires when a form is submitted.
keydownFires when a keyboard key is pressed down.
mouseoverFires when the mouse pointer moves over an element.
changeFires when the value of a select, checkbox, or radio input is changed and confirmed.
focusFires when an element receives focus (e.g. clicking into an input field).
blurFires when an element loses focus.