JavaScript DOM Manipulation Methods Cheat Sheet
DOM (Document Object Model) manipulation is essential for web development, allowing you to dynamically change the content and structure of web pages. This cheat sheet covers various methods for interacting with the DOM.
Selecting Elements
getElementById()
Selects an element by its ID.
getElementsByClassName()
Selects elements by their class name.
getElementsByTagName()
Selects elements by their tag name.
querySelector()
Selects the first element that matches a CSS selector.
querySelectorAll()
Selects all elements that match a CSS selector.
Creating Elements
createElement()
Creates a new element.
Modifying Elements
innerHTML
Sets or gets the HTML content of an element.
textContent
Sets or gets the text content of an element.
setAttribute()
Sets the value of an attribute on an element.
getAttribute()
Gets the value of an attribute on an element.
removeAttribute()
Removes an attribute from an element.
classList.add()
Adds one or more class names to an element.
classList.remove()
Removes one or more class names from an element.
classList.toggle()
Toggles a class name on an element.
style
Sets or gets the inline style of an element.
Adding and Removing Elements
appendChild()
Appends a new child element to a parent element.
insertBefore()
Inserts a new element before an existing child element.
removeChild()
Removes a child element from a parent element.
replaceChild()
Replaces a child element with a new element.
Event Handling
addEventListener()
Attaches an event handler to an element.
removeEventListener()
Removes an event handler from an element.
Traversing the DOM
parentNode
Gets the parent node of an element.
childNodes
Gets the child nodes of an element.
firstChild
Gets the first child of an element.
lastChild
Gets the last child of an element.
nextSibling
Gets the next sibling of an element.
previousSibling
Gets the previous sibling of an element.
firstElementChild
Gets the first child element of a parent element.
lastElementChild
Gets the last child element of a parent element.
nextElementSibling
Gets the next sibling element of an element.
previousElementSibling
Gets the previous sibling element of an element.
Manipulating Forms
value
Gets or sets the value of a form element.
submit()
Submits a form programmatically.
reset()
Resets a form.
This comprehensive overview covers the most commonly used DOM manipulation methods in JavaScript, providing a quick reference for web developers.