IntroductionCopy link
The DOM, or Document Object Model, provides methods to add, change, and remove HTML elements using JavaScript.
Prerequisites
- HTML Elements, Variables, Strings, Objects
Each section will first show the code with the word placeholder, followed by examples of what can replace the placeholder. Next, there will be examples of how to use this method. This will usually involve using a variable, just remember that the variable names are your choice.
Here's a table of the methods detailed in this article.
Goal | Method |
---|---|
Select | document.getElementById('your-element-id'); document.getElementById('your-element-id'); |
Add a class | yourHtmlElement.classList.add('your-class'); yourHtmlElement.classList.add('your-class'); |
Remove a class | yourHtmlElement.classList.remove('your-class'); yourHtmlElement.classList.remove('your-class'); |
Create | document.createElement('div'); document.createElement('div'); |
Add to the page | yourParentElement.appendChild(yourHtmlElement); yourParentElement.appendChild(yourHtmlElement); |
Remove from the page | yourParentElement.removeChild(yourHtmlElement); yourParentElement.removeChild(yourHtmlElement); |
Adjust the inner HTML | yourHtmlElement.innerHTML = 'Your new text'; yourHtmlElement.innerHTML = 'Your new text'; |
Select an HTML elementCopy link
document.getElementById(placeholder);
document.getElementById(placeholder);
- Replace
placeholder
placeholder
with a string that matches the id of the element you want to select, such as'about'
'about'
or'menu-item'
'menu-item'
.
ExamplesCopy link
The code below is part of the HTML file.
index.html
<body> <h1>Hello</h1> <div id="about-section"></div> </body>
index.html
<body> <h1>Hello</h1> <div id="about-section"></div> </body>
The code below selects the <div>
<div>
element on line 6 using its id 'about-section'
'about-section'
and stores it in a variable named about
about
.
script.js
const about = document.getElementById('about-section');
script.js
const about = document.getElementById('about-section');
Add a Class to an HTML ElementCopy link
placeholder1.classList.add(placeholder2)
placeholder1.classList.add(placeholder2)
- Replace
placeholder1
placeholder1
with your HTML element. - Replace
placeholder2
placeholder2
with the class you want to add as a string, such as'container'
'container'
or'serif-header'
'serif-header'
.
ExamplesCopy link
The code below is in the HTML file.
index.html
<body> <h1 id="greeting">Hello</h1> <div id="home"></div> </body>
index.html
<body> <h1 id="greeting">Hello</h1> <div id="home"></div> </body>
To add the 'serif-header'
'serif-header'
class to the <h1>
<h1>
on line 2, use the code below.
script.js
// select the h1 by it's id const h1Element = document.getElementById('greeting'); // add a class to the h1 h1Element.classList.add('serif-header')
script.js
// select the h1 by it's id const h1Element = document.getElementById('greeting'); // add a class to the h1 h1Element.classList.add('serif-header')
Alternatively, you can do this without a variable.
script.js
// select the element add a class document.getElementById('greeting').classList.add('serif-header');
script.js
// select the element add a class document.getElementById('greeting').classList.add('serif-header');
After either of the above JavaScript code examples run, the HTML in the browser will be updated. This will not change the HTML code you wrote in your code editor.
index.html
<body> <h1 id="greeting" class="serif-header">Hello</h1> <div id="home"></div> </body>
index.html
<body> <h1 id="greeting" class="serif-header">Hello</h1> <div id="home"></div> </body>
Remove a Class from an HTML ElementCopy link
placeholder1.classList.remove(placeholder2)
placeholder1.classList.remove(placeholder2)
- Replace
placeholder1
placeholder1
with your HTML element. - Replace
placeholder2
placeholder2
with the class you want to remove as a string, such as'container'
'container'
or'serif-header'
'serif-header'
.
ExamplesCopy link
The code below is in the HTML file.
index.html
<body> <h1 id="greeting" class="serif-header">Hello</h1> <div id="home"></div> </body>
index.html
<body> <h1 id="greeting" class="serif-header">Hello</h1> <div id="home"></div> </body>
To remove the 'serif-header'
'serif-header'
class from the <h1>
<h1>
on line 2, use the code below.
script.js
// select the h1 by it's id const h1Element = document.getElementById('greeting'); // remove a class from the h1 h1Element.classList.remove('serif-header')
script.js
// select the h1 by it's id const h1Element = document.getElementById('greeting'); // remove a class from the h1 h1Element.classList.remove('serif-header')
Alternatively, you can do this without a variable.
script.js
// select the element remove a class document.getElementById('greeting').classList.remove('serif-header');
script.js
// select the element remove a class document.getElementById('greeting').classList.remove('serif-header');
After either of the above JavaScript code examples run, the HTML in the browser will be updated. This will not change the HTML code you wrote in your code editor.
index.html
<body> <h1 id="greeting">Hello</h1> <div id="home"></div> </body>
index.html
<body> <h1 id="greeting">Hello</h1> <div id="home"></div> </body>
Create an HTML ElementCopy link
document.createElement(placeholder);
document.createElement(placeholder);
- replace
placeholder
placeholder
with an HTML tag as a string, such as'div'
'div'
,'img'
'img'
, or'section'
'section'
ExamplesCopy link
The code below creates a new <div>
<div>
element and stores it in a variable named newElement
newElement
. The element is not on the page yet.
script.js
const newElement = document.createElement('div');
script.js
const newElement = document.createElement('div');
The code below creates a new <p>
<p>
element and stores it in a variable named newParagraph
newParagraph
. The element is not on the page yet.
script.js
const newParagraph = document.createElement('p');
script.js
const newParagraph = document.createElement('p');
Add an Element to the PageCopy link
placeholder1.appendChild(placeholder2)
placeholder1.appendChild(placeholder2)
- Replace
placeholder1
placeholder1
with your parent element, or the element that you want your element to be inside of. - Replace
placeholder2
placeholder2
with your child element, or the element that will go inside the other element.
ExamplesCopy link
The code below is part of the HTML file.
index.html
<div id="about-section"> </div> <div id="contact-section"> </div>
index.html
<div id="about-section"> </div> <div id="contact-section"> </div>
To add a new <p>
<p>
element to the about section, use the code below.
script.js
// create the new element const newParagraph = document.createElement('p'); // select the parent element const aboutSection = document.getElementById('about-section'); // add the new element to the parent element aboutSection.appendChild(newParagraph);
script.js
// create the new element const newParagraph = document.createElement('p'); // select the parent element const aboutSection = document.getElementById('about-section'); // add the new element to the parent element aboutSection.appendChild(newParagraph);
After the code above is executed, the HTML in the browser will be updated. This will not change the HTML code written in your code editor.
index.html
<div id="about-section"> <p></p> </div> <div id="contact-section"> </div>
index.html
<div id="about-section"> <p></p> </div> <div id="contact-section"> </div>
Remove an Element from the PageCopy link
placeholder1.removeChild(placeholder2)
placeholder1.removeChild(placeholder2)
- Replace
placeholder1
placeholder1
with your parent element, or the element that surrounds the element you wish to remove. - Replace
placeholder2
placeholder2
with your child element, or the element that you want to remove from the page.
ExamplesCopy link
The code below is part of the HTML file.
index.html
<div id="about-section"> <p id="about-paragraph">This is a paragraph in the about section.</p> </div> <div id="contact-section"> </div>
index.html
<div id="about-section"> <p id="about-paragraph">This is a paragraph in the about section.</p> </div> <div id="contact-section"> </div>
To remove the <p>
<p>
element on line 21, use the code below.
script.js
// select the element to be removed const paragraph = document.getElementById('about-paragraph'); // select the parent element const aboutSection = document.getElementById('about-section'); // remove the paragraph from the about section aboutSection.removeChild(paragraph);
script.js
// select the element to be removed const paragraph = document.getElementById('about-paragraph'); // select the parent element const aboutSection = document.getElementById('about-section'); // remove the paragraph from the about section aboutSection.removeChild(paragraph);
After the code above is executed, the HTML in the browser will be updated. This will not change the HTML code written in your code editor.
index.html
<div id="about-section"> </div> <div id="contact-section"> </div>
index.html
<div id="about-section"> </div> <div id="contact-section"> </div>
Adjust the innerHTML of an ElementCopy link
- Set the inner HTML:
placeholder1.innerHTML = placeholder2;
placeholder1.innerHTML = placeholder2;
- Replace
placeholder1
placeholder1
with the element whoseinnerHTML
innerHTML
you want to adjust. - Replace
placeholder2
placeholder2
with your desired HTML code as a string.
- Get the inner HTML:
let placeholder3 = placeholder4.innerHTML
let placeholder3 = placeholder4.innerHTML
- Replace
placeholder3
placeholder3
with a variable name that will store the inner HTML. - Replace
placeholder4
placeholder4
with the HTML element whose inner HTML you want to access.
ExamplesCopy link
The code below is part of the HTML file.
index.html
<div id="about-section"> <p id="about-paragraph">This is a paragraph in the about section.</p> </div> <div id="contact-section"> </div>
index.html
<div id="about-section"> <p id="about-paragraph">This is a paragraph in the about section.</p> </div> <div id="contact-section"> </div>
To edit the innerHTML
innerHTML
of the contact section, use the code below.
script.js
document.getElementById('contact-section').innerHTML = '<h1>Contact</h1>';
script.js
document.getElementById('contact-section').innerHTML = '<h1>Contact</h1>';
After the code above is executed, the HTML in the browser will be updated. This will not change the HTML code written in your code editor.
index.html
<div id="about-section"> <p id="about-paragraph">This is a paragraph in the about section.</p> </div> <div id="contact-section"> <h1>Contact</h1> </div>
index.html
<div id="about-section"> <p id="about-paragraph">This is a paragraph in the about section.</p> </div> <div id="contact-section"> <h1>Contact</h1> </div>
To get the innerHTML
innerHTML
of an element, use the code below.
script.js
let pText = document.getElementById('about-paragraph').innerHTML; console.log(pText);
script.js
let pText = document.getElementById('about-paragraph').innerHTML; console.log(pText);
The variable pText
pText
in the code above is now storing the innerHTML
innerHTML
of the paragraph in the about section. The output to the console would read: "This is a paragraph in the about section."