DOM Manipulation
The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the structure of a document as a tree of objects, allowing developers to manipulate the content and visual presentation of an online document with scripts such as JavaScript. Here is a cheat sheet for JavaScript DOM manipulation covering key subtopics.
What is the DOM?
Section titled “What is the DOM?”The DOM is an object-oriented representation of the web page, which can be modified with a scripting language such as JavaScript. It represents the document as nodes and objects, allowing programming languages to interact with the page.
// Log the entire body elementconsole.log(document.body);In this code snippet, document.body is a representation of the <body> tag as an object in the DOM. You can perform actions on this object to change the structure, style, or content of the <body> element.
Accessing elements
Section titled “Accessing elements”getElementBy… selectors
Section titled “getElementBy… selectors”JavaScript provides several methods to access DOM elements using their id, class name, tag name, etc. These methods include getElementById, getElementsByClassName, getElementsByTagName, among others.
// Access an element by its IDvar titleElement = document.getElementById('title');
// Access elements by their class namevar importantElements = document.getElementsByClassName('important');
// Access elements by their tag namevar paragraphElements = document.getElementsByTagName('p');In the example above, we’re using getElementById to access an element with the ID of ‘title’. The getElementsByClassName and getElementsByTagName methods return a live HTMLCollection of elements with the specified class name and tag name, respectively.
querySelector
Section titled “querySelector”The querySelector method allows you to find the first element that matches a specified CSS selector(s) in the document.
// Select the first .important elementvar importantElement = document.querySelector('.important');This code selects the first element in the document that has a class of ‘important’.
querySelectorAll
Section titled “querySelectorAll”The querySelectorAll method works similarly to querySelector, but instead of returning the first match, it returns a NodeList containing all elements that match the specified CSS selector(s).
// Select all .important elementsvar allImportantElements = document.querySelectorAll('.important');This snippet selects all elements in the document with the class ‘important’.
Parents, childs and siblings
Section titled “Parents, childs and siblings”Every element in the DOM has a relationship with other elements. These relationships are categorized as parent, child, and sibling relationships.
// Select the parent of an elementvar parentElement = document.querySelector('.important').parentNode;
// Select the children of an elementvar childElements = document.querySelector('.important').children;
// Select the next sibling of an elementvar nextSiblingElement = document.querySelector('.important').nextElementSibling;
// Select the previous sibling of an elementvar prevSiblingElement = document.querySelector('.important').previousElementSibling;In this example, we’re using properties like parentNode, children, nextElementSibling, and previousElementSibling to access related elements in the DOM.
Modifying elements
Section titled “Modifying elements”Element content
Section titled “Element content”To change the content of a DOM element, you can use the innerText or innerHTML property.
// Change the text of an elementdocument.getElementById('title').innerText = 'New Title';
// Change the HTML of an elementdocument.getElementById('title').innerHTML = '<em>New Title</em>';In this example, innerText changes the text content of the element with the ID ‘title’, while innerHTML changes the HTML content, allowing for the inclusion of HTML tags.
Element attributes
Section titled “Element attributes”Attributes of a DOM element, such as ‘class’, ‘id’, ‘href’ and ‘src’ can be accessed and modified using the getAttribute, setAttribute, removeAttribute, and hasAttribute methods.
var linkElement = document.querySelector('a');
// Get the href attribute of a linkvar href = linkElement.getAttribute('href');
// Set a new href for the linklinkElement.setAttribute('href', 'https://www.new-url.com');
// Check if the link has a target attributevar hasTarget = linkElement.hasAttribute('target');
// Remove the target attribute from the linklinkElement.removeAttribute('target');In this snippet, we first access the href attribute of a link, then change it using setAttribute. We also check if the target attribute exists on the link and remove it.
Element CSS classes
Section titled “Element CSS classes”You can manipulate the CSS classes of a DOM element using the classList property, which provides methods like add, remove, toggle, and contains.
var titleElement = document.querySelector('#title');
// Add a new class to the elementtitleElement.classList.add('new-class');
// Remove a class from the elementtitleElement.classList.remove('old-class');
// Toggle a class on the elementtitleElement.classList.toggle('toggle-class');
// Check if the element has a specific classvar hasClass = titleElement.classList.contains('check-class');Here, we add a new CSS class to an element, remove an existing class, toggle a class (if the class exists it is removed; if it does not exist, it is added), and check if an element has a specific class.
Creating new elements
Section titled “Creating new elements”createElement
Section titled “createElement”You can create new elements in the DOM using the createElement method.
// Create a new div elementvar newElement = document.createElement('div');This code creates a new <div> element. However, it’s important to note that the new element is not yet added to the DOM tree.
appendChild
Section titled “appendChild”The appendChild method is used to append a node as the last child of a node.
// Create a new paragraph elementvar newElement = document.createElement('p');newElement.innerText = 'This is a new paragraph';
// Append the new element to the bodydocument.body.appendChild(newElement);In this example, we create a new paragraph element, set its text, and append it to the <body> element.
insertBefore
Section titled “insertBefore”The insertBefore method inserts a node before a reference node as a child of a specified parent node.
// Create a new paragraph elementvar newElement = document.createElement('p');newElement.innerText = 'This is a new paragraph';
// Get the first child of the bodyvar firstChild = document.body.firstChild;
// Insert the new element before the first child of the bodydocument.body.insertBefore(newElement, firstChild);This snippet creates a new paragraph and adds it to the beginning of the <body> element.
cloneNode
Section titled “cloneNode”The cloneNode method clones a node, and returns the clone.
// Clone an existing elementvar titleElement = document.querySelector('#title');var clonedElement = titleElement.cloneNode(true);
// Append the cloned element to the bodydocument.body.appendChild(clonedElement);This code snippet clones the element with the ID ‘title’ and appends the clone to the end of the <body> element. The cloneNode(true) method clones all descendants of the node, while cloneNode(false) clones only the node itself.
Event handling
Section titled “Event handling”Events are actions or occurrences that happen in the system you are programming. The system notifies you of such events via events. JavaScript allows you to define behaviors that should be performed when such events occur.
// Select an elementvar buttonElement = document.querySelector('button');
// Define an event handler functionvar handleClick = function(event) { console.log('Button clicked!', event);};
// Attach the event handler to the buttonbuttonElement.addEventListener('click', handleClick);In this snippet, we’re attaching an event handler to a button element. The handleClick function is executed every time a ‘click’ event is fired on the button.
Styling DOM nodes
Section titled “Styling DOM nodes”DOM nodes can be styled using JavaScript by manipulating their style property.
// Select an elementvar titleElement = document.querySelector('#title');
// Change the element's colortitleElement.style.color = 'red';
// Change the element's background colortitleElement.style.backgroundColor = 'black';In this example, we’re changing the text color and the background color of an element. Each CSS property is a sub-property of style.
DOM traversal
Section titled “DOM traversal”DOM traversal involves navigating around the DOM tree using properties like parentNode, firstChild, lastChild, nextSibling, and previousSibling.
// Select an elementvar titleElement = document.querySelector('#title');
// Get the parent of the elementvar parent = titleElement.parentNode;
// Get the first child of the elementvar firstChild = titleElement.firstChild;
// Get the last child of the elementvar lastChild = titleElement.lastChild;
// Get the next sibling of the elementvar nextSibling = titleElement.nextSibling;
// Get the previous sibling of the elementvar prevSibling = titleElement.previousSibling;In this code snippet, we’re navigating around the DOM tree using various DOM traversal properties. These properties provide a way to navigate through the child, parent, and sibling nodes.
Performance considerations
Section titled “Performance considerations”Manipulating the DOM can be slow and can lead to performance issues if not done wisely. Therefore, it is advisable to minimize direct DOM manipulation, and batch changes where possible.
// Create a document fragmentvar fragment = document.createDocumentFragment();
// Create and append elements to the fragmentfor (var i = 0; i < 1000; i++) { var newElement = document.createElement('p'); newElement.innerText = 'This is paragraph number ' + i; fragment.appendChild(newElement);}
// Append the fragment to the bodydocument.body.appendChild(fragment);In this example, we use a DocumentFragment to create and append 1000 new paragraph elements. Appending the DocumentFragment to the DOM triggers a single reflow, rather than 1000, improving performance.