What did we learn the last time?

DOM - part 1

We have writing in the console. But now are we going to write on the site. But first I will tell something about the DOM.

DOM stands for Document Object Model and can define HTML elements as methods, gives them properties and events. The DOM can change the document structure, design and content.

When you open a website in the browser, parser the browser the HTML site. The browser builts the document structure and uses the DOM to draw on the screen.JavaScript uses the DOM to get acces to the document and its elements. The DOM is not a programming languange, but without it does JavaScript not know to websites og HTML documents. Every element in the document is a part of the Document Object Model

The DOM tree

For the DOM tree is every built HTML tag an object, what you can manipulate though JavaScript. The browser builts the tree when the site is loading. In the three is the lements defined as nodes which has the content of objects

JS DOM tree


The yellow boxes are the root elements. They are on the top of the hiearki.
The document is alle websites, that will be loaded in the browser. Every element in the three is represented by a DOM node.
The DOM includes elements as the body tag or the h1 tag


The blue boxes are element nodes which describes the structure of an HTML site.


The pink boxes are attribute nodes which are HTML elements which contain attributes.


The green boxes are text nodes which is the text outside an element.

Example on the DOM tree

<!doctype html> 
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<button id="JSbutton" style="background-color:white; outline:1px solid purple; border: none; padding: 10px;">What happens?</button>
<script src="../js/number.js"> </script>
Example 3.6.1
document.getElementById("JSbutton").addEventListener('click', function() { 
document.getElementById("JSbutton").style.backgroundColor = "red";
document.getElementById("JSbutton").style.color = "white";
Example 3.6.2

DOM nodes

Now we will talk about DOM nodes - The blue ones from the DOM tree

The document and it's properties

The document is an object, and each object can have some methods and properties attached.

Example 3.6.3

What do you get if you write it in the console?

Accessing the elements on the page

You can manipulate HTML elements on the site with DOM nodes.

This is a part from a the HTML site we will take as a starting point.

<img id="section-img" src="" alt="Mountain from top taking by Jeff Hopper">
<h1 id="mainHeading">This is a heading</h1>
<p>Some text</p>
<li>We can write on the site with the use of DOM nodes</li>
<li>We can change elements with the use of DOM nodes</li>
<li>We can make elements with the use of DOM nodes</li>
Example 3.6.4


The easiest way to find an HTML element in the DOM, is by using the element id. If the element is found will the method return an element that you can manipulate. If it can not find the elements will it return null

const h1 = document.getElementById("mainHeading"); //get the element based on the id 
console.log(h1); //writes the element
Example 3.6.5


With the document.getElementById(id); we can grab the element, but we see what where are inside the element.

console.log(h1.innerHTML); //gives us the content
h1.innerHTML = "This is JavaScript DOM nodes"; //changes the content
Example 3.6.6

We can now change the content, but what about CSS?
You can also change that. To do it you can change the style instructions of the element, accessing the .style property. = "darkblue"; //change the font color = "lightgreen"; //change the background color
Example 3.6.7


You can find elements with the tag name. The nodes can be accessed by index numbers

const li = document.getElementsByTagName("li"); //find all the elements with that tag
console.log(li); //writes alle the tags
console.log(li[0].innerHTML); //write the content of that one tag
Example 3.6.8

document.querySelector(selectors); method

If you do not want to use document.getElementById(); then use the .querySelector() method. By using the .querySelector() method, we cannot only access the elements that has id attribute, but practically every element on the page.

const image = document.querySelector("#section-img"); //gets the image
console.log(image); //write it in the console
Example 3.6.9


You can also change the attribute with the the attribute property

image.src = "";
Example 3.6.10


This method creates an element with the specified name, and returns the element.

const btn = document.createElement("button"); //create element
Example 3.6.11

Can you see the button on the page?
Can you see it with help from JavaScript?

console.log(btn); //find the element with JS
btn.innerHTML = "CLICK ME"; //gives it some text inside 
console.log(btn); //find the element with JS
Example 3.6.12

document.setAttribute(attribute, value)

This method creates an attribute with the specified name, and gives it the specified value.

let btnClass = btn.setAttribute("id", "button");
Example 3.6.13


This method adds a node to the specified parent node.

Example 3.6.14

target.addEventListener(type, function)

The EventTarget sets up a function that will be called when the specified event is delivered to the target.

document.getElementById("button").addEventListener('click', function() { 
console.log("you pressed the button");
Example 3.6.15

Dom nodes - Part 2

There are more DOM nodes

Accessing the elements on the page

You can manipulate HTML elements on the site with DOM nodes.

<section class="section-form"> 
<form id="form" action="#" method="post">
<input type="text" id="name">
<button type="submit" value="submit">Send</button>
<p id="formText"></p>
Example 3.6.16


If you want to find all elements with the class name you can use this method.

const sectionForm = document.getElementsByClassName("section-form"); //get the element 
sectionForm[0].style.backgroundColor = "pink"; //change the background color
Example 3.6.17


If you have an input field and want the information from the input field can you use value property.

const form = document.getElementById('form'); //get the form element 
const formText = document.getElementById('formText'); //get the paragraph, so we can change the value
const name = document.getElementById("name"); //get the inputfield

function submitPushed(event) { //make a function
formText.innerHTML = `${name.value}, you pushed the form`; ///change the value inside the div
event.preventDefault(); //tells the form is invalid

form.addEventListener('submit', submitPushed); //Adds a eventlistener
Example 3.6.18

A little help

To save keywords can you write this as the first on the document

// save keystrokes for id
const $ = function (foo) {
return document.getElementById(foo);
// save keystrokes for tagName
const tag$ = function (foo) {
return document.getElementsByTagName(foo);
// save keystrokes for className
const class$ = function (foo) {
return document.getElementsByClassName(foo);
Example 3.6.19


//without the ID function
document.getElementById("JSbutton").style.backgroundColor = "red";

//with the ID function
$("JSbutton").style.backgroundColor = "red";

//without the tagName function
let tagName = document.getElementsByTagName("li");

//with the tagName function
let tagName = tag$("li");

//without the ClassName function
let className = document.getElmenetsByClassName("className");

//with the classNamefunction
let className = class$("className");
Example 3.6.20

How can we use JavaScript?

Go up button

'use strict';
const body = document.getElementsByTagName("body")[0];
const button = document.createElement('button');
const textnode = document.createTextNode('To the top');
button.setAttribute("id", "topBtn");

// When the user scrolls down 20px from the top of the document, show the button
document.addEventListener('scroll', function(e){
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { = "block";
} else { = "none";

// When the user clicks on the button, scroll to the top of the document
button.addEventListener("click", function() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
Example 3.6.21


See the Pen Images by Dominoeffekten (@dominoeffekten) on CodePen.

<img id="bannerImage" src="">
width: 70%;
'use strict';
var images = [
var text = ["butterfly", "fish", "frog"]
var i = 0;

var renew = setInterval(function(){
if(images.length == i){
i = 0;
} else {
document.getElementById("bannerImage").src = images[i];
document.getElementById("bannerImage").setAttribute("title", text[i]);
document.getElementById("bannerImage").setAttribute("alt", text[i]);
Example 3.6.22

Modal box

See the Pen ModalBox by Dominoeffekten (@dominoeffekten) on CodePen.

<section id="container" class="baconIpsum">
<h2>Bacon Ipsum</h2>
<p>Bacon ipsum dolor amet t-bone pancetta tongue hamburger, bacon spare ribs burgdoggen short ribs filet mignon chislic picanha turducken.</p>
<button id="next">Next</button>

<section id="modalBox" class="zombieIpsum">
<span id="close">X</span>
<h2>Zombie ipsum</h2>
<p>Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro. De carne lumbering animata corpora quaeritis.</p>
display: none;
position: absolute;
background-color: coral;
margin: 20px;

cursor: pointer;
//get the modal element
let modal = document.getElementById("modalBox");
//get the container elemenet
let container = document.getElementById("container");
// Get the element that closes the modal
var close = document.getElementById("close");

// When the user clicks the button, open the modal
function openModal(){ = "block";

// close the modalbox
function closeModal(event){ = "none";

document.getElementById("next").addEventListener("click", openModal);
close.addEventListener("click", closeModal);
Example 3.6.22