You need to create elements with createElement before you can add them to the DOM.
createElement
createElement takes in one value—the tag of the element you want to create.
const paragraph = document.createElement('p')const div = document.createElement('div')You can change your element’s contents and attributes after creating it.
paragraph.classList.add('red')paragraph.textContent = `I'm red!`Adding elements to the DOM
You can use two methods to add elements to the DOM:
appendChildinsertBefore
appendChild
appendChild adds your element as the final child of the specified element. It takes in one argument—the element you want to append.
parentElement.appendChild(newElement)Let’s see it in action. Say you have the following HTML.
<ol> <li>Banana</li> <li>Pineapple</li> <li>Orange</li></ol>You want to add another item, Strawberry, to the end of the list. Here’s how you’d do it with appendChild.
// Create Strawberryconst li = document.createElement('li')li.textContent = 'Strawberry'
// Append Strawberryconst list = document.querySelector('ol')list.appendChild(li)Here’s the result:
<ol> <li>Banana</li> <li>Pineapple</li> <li>Orange</li> <li>Strawberry</li></ol>appendChild has a wonky behavior—if the element you want to append already exists in your DOM, appendChild moves the element from its existing location to the new location you specified.
<ol class="fruits-i-like"> <li>Banana</li> <li>Pineapple</li> <li>Orange</li></ol>
<ol class="fruits-i-dont-like"> <li>Strawberry</li></ol>const orange = document.querySelectorAll('li')[2]
const dontLike = document.querySelector('.fruits-i-dont-like')dontLike.appendChild(orange)The result is:
<ol class="fruits-i-like"> <li>Banana</li> <li>Pineapple</li></ol>
<ol class="fruits-i-dont-like"> <li>Strawberry</li> <li>Orange</li></ol>Don’t use appendChild to move elements because you wouldn’t expect this behavior. If you want to move an element, delete it before moving it. Be explicit.
You’ll learn how to delete an element in a later lesson.
insertBefore
insertBefore adds an element before another element. It has the following syntax:
parentElement.insertBefore(newElement, referenceElement)newElementis the element you want to insert.referenceElementtells browsers where to insertnewElement.newElementwill be inserted just beforereferenceElement.
Let’s see this in action. Say you have the following HTML:
<ol> <li>Banana</li> <li>Pineapple</li> <li>Orange</li></ol>If you want to insert Strawberry as the first list item, you need to set Banana as your referenceElement.
// Create strawberry nodeconst strawberry = document.createElement('li')strawberry.textContent = 'Strawberry'
// Add strawberry node before bananaconst list = document.querySelector('ol')const banana = list.children[0]list.insertBefore(strawberry, banana)The result will be:
<ol> <li>Strawberry</li> <li>Banana</li> <li>Pineapple</li> <li>Orange</li></ol>Exercise
Practice creating and adding elements to the DOM. Say you have the following HTML
<div class="characters"> <ul class="hobbits"> <li>Frodo Baggins</li> <li>Samwise "Sam" Gamgee</li> <li>Meriadoc "Merry" Brandybuck</li> <li>Peregrin "Pippin" Took</li> </ul> <ul class="elves"> <li>Glorfindel</li> <li>Elrond</li> <li>Arwen Evenstar</li> </ul> <ul class="humans"> <li>Gandalf</li> <li>Saruman</li> <li>Boromir</li> <li>Faramir</li> </ul></div>- Create a list item,
<li>Bilbo Baggins</li>, and add it as the last item in.hobbits. - Create a list item,
<li>Legolas</li>, and insert it as the first item in.elves. - Create a list item,
<li>Aragorn</li>, and insert it before<li>Boromir</li>.
Create a list item, <li>Bilbo Baggins</li>, and add it as the last item in .hobbits
const bilbo = document.createElement('li')bilbo.textContent = 'Bilbo Baggins'
const hobbits = document.querySelector('.hobbits')hobbits.appendChild(bilbo)Create a list item, <li>Legolas</li>, and insert it as the first item in .elves.
const legolas = document.createElement('li')legolas.textContent = 'Legolas'
const elves = document.querySelector('.elves')elves.insertBefore(legolas, elves.children[0])Create a list item, <li>Aragorn</li>, and insert it before <li>Boromir</li>.
const aragon = document.createElement('li')aragon.textContent = 'Aragon'
const humans = document.querySelector('.humans')const boromir = humans.children[2]humans.insertBefore(aragon, boromir)Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson