DOM Manipulation in JavaScript
DOM Manipulation in JavaScript
DOM stands for Document Object Model. But what does that mean? Let's break it down.
The Document part refers to the webpage you see in the browser. Specifically, the
HTML Document which handles the structure of the page's content. This includes the
text, images, links, and other elements that make up the page.
Object means the elements like images, headers, and paragraphs are treated like
objects. Each object has its properties (like id, class, style) and methods. Using these
properties and methods, you can manipulate the elements.
The Model in DOM means it's a representation or copy of the HTML document as a
hierarchical tree. This tree includes all the elements. And it captures the parent-child
relationships between them.
The DOM is always identical to the HTML document. Browsers ensure that they are in
sync. So, if something changes in the HTML, the DOM changes too, and vice versa.
Let's use the following phonebook markup to show how the various DOM selector
methods work.
<h1 id="page-title">Phonebook</h1>
<p class="family">Marie</p>
<p class="family">Jose</p>
<p class="work">Anne</p>
<p class="work">Joan</p>
The markup includes a header with an id of page-title and four paragraphs. The first two
paragraphs both have a class of family, and the last two have a class of work.
1. getElementById
You use this method to select elements with an id attribute. Ids are unique identifiers.
For example, if a header element has an id attribute with a value of "page-title", no
other element on the page should also have an id with the same value.
This means anytime you use the getElementById method, you are going to select only
one element from the DOM.
Let's look at an example:
The h1 header has an id value of page-title. Here is how you can select it using
the getElementById method:
const titleElement = document.getElementById("page-title")
console.log(titleElement)
The example selects the header element and assigns it to the titleElement variable.
2. getElementsByClassName
You can use this method to select more than one object. This method takes in the value
of a class attribute as an argument and selects all elements in the DOM that has the
given class. Unlike ids, you can give the same class value for different HTML elements.
Here's an example:
3. getElementsByTagName
This method will select elements using their tag name. For
example, getElementByTagName('p') will select all paragraphs on the page.
Like getElementsByClassName, this method also returns an HTML collection of the
selected elements.
Here's an example:
5. querySelectorAll
Like the querySelector method, querySelectorAll also selects HTML elements using CSS
selectors. The difference is that it returns all elements that match the selector instead of
returning only the first one.
Using the previous example, let's select all the buttons with querySelectorAll.
const allBtns = document.querySelectorAll('button')
console.log(allBtns)
The first thing you need to do is to select the element. You can do that using any of the
methods you learned from the previous section.
After selecting the element, there are several methods you can use to add or update the
content.
The topic is updated from "JS Array methods" to "JavaScript array methods"
With innerHTML, you can change more than just the content. You can also change the
HTML structure of the element. For example, if you want to make the word "JavaScript"
bold, you could do this:
topicElement.innerHTML = "<b>JavaScript</b> array methods"
A paragraph element with the some text and a hidden span element inside
The example includes a paragraph element. Inside the paragraph is a span that contains
a key. The key does not appear on screen because its inline style is set to a display of
none.
Now, let's select the paragraph and print both the innerText value and textContent value
to see the difference.
const paragraph = document.querySelector('p');
<p>
<b>innerText Example</b>
<span id="inner-text"></span>
</p>
<hr>
<p>
<b>textContent Example</b>
<span id="textContent"></span>
</p>
let address = `
ABC Street,
Spintex Road,
Accra,
Ghana.
`;
example1.innerText = address;
example2.textContent = address;
The code gives the same variable address to the two examples. The first
uses innerText and the second uses textContent. See the results below:
Conclusion
DOM manipulation in JavaScript is an important factor while creating a web application
using HTML and JavaScript. It is the process of interacting with the DOM API to change
or modify an HTML document that will be displayed in a web browser. This HTML
document can be changed to add or remove elements, update existing elements,
rearrange existing elements, etc.
By manipulating the DOM, we can create web applications that update the data in a web
page without refreshing the page and can change its layout without doing a refresh.
Throughout the document, items can be deleted, moved, or rearranged.