What is the DOM?
HTML elements as objects
Properties and events of the HTML elements
Methods to access the HTML elements
The places of the elements are referred to as nodes. Not only elements get nodes but also the attributes of elements and text get their own node (attribute-nodes and text-nodes).
The DOM Document is the owner of all other objects in your webpage. That means if you want to access any object on your webpage you always have to start with the document. It also contains many important properties and methods that enable us to access and modify our website.
Finding HTML Elements
Get element by ID
The getElementById() method is used to get a single element by its id. Let’s look at an example:
var title = document.getElementById(‘header-title’);
Here we get the element with the id of header-title and save it into a variable.
Get elements by class name
We can also get more than one object using the getElementsByClassName() method which returns an array of elements.
var items = document.getElementsByClassName(‘list-items’);
Here we get all items with the class list-items and save them into a variable.
Get element by tag name
We can also get our elements by tag name using the getElementsByTagName() method.
var listItems = document.getElementsByTagName(‘li’);
Here we get all li elements of our HTML document and save them into a variable.
The querySelector() method returns the first element that matches a specified CSS selector. That means that you can get elements by id, class, tag and all other valid CSS selectors. Here I just list a few of the most popular options.
Get by id:
var header = document.querySelector(‘#header’)
Get by class:
var items = document.querySelector(‘.list-items’)
Get by tag:
var headings = document.querySelector(‘h1’);
Get more specific elements:
We can also get more specific elements using CSS Selectors.
In this example we search for a tag and a class at the same time and return the first element that passes the CSS Selector.
The querySelectorAll() method is completely the same as the querySelector() except that it returns all elements that fit the CSS Selector.
var heading = document.querySelectorAll(‘h1.heading’);
In this example, we get all h1 tags that have a class of heading and store them in an array.
Changing HTML Elements
The HTML DOM allows us to change the content and style of an HTML element by changing its properties.
Changing the HTML
The innerHTML property can be used to change the content of an HTML element.
The CSS properties need to be written in camelcase instead of the normal css property name. In this example we used borderBottom instead of border-bottom.
Adding and deleting elements
Now we will take a look at how we can add new elements and delete existing ones.
var div = document.createElement(‘div’);
Here we just create a div element using the createElement() method which takes a tagname as a parameter and saves it into a variable. After that we just need to give it some content and then insert it into our DOM document.
var newContent = document.createTextNode("Hello World!");
Here we create content using the createTextNode() method which takes a String as a parameter and then we insert our new div element before a div that already exists in our document.
var elem = document.querySelector('#header');
Here we get an element and delete it using the removeChild() method.
Now let’s take a look at how we can replace items.
var div = document.querySelector('#div');
var newDiv = document.createElement(‘div’);
newDiv.innerHTML = "Hello World2"
Here we replace an element using the replaceChild() method. The first argument is the new element and the second argument is the element which we want to replace.
Writing directly into the HTML output stream
document.write(“<h1>Hello World!</h1><p>This is a paragraph!</p>”);
The write() method can also take multiple arguments that will be appended to the document in order of their occurrence.
input field change
You can define events directly in your HTML code using attributes on your tags. Here is an example of an onclick event:
The nodes in the DOM Document have a hierarchical relationship to each other. This means that the nodes are structured like a tree. We use the terms parent, sibling and child to describe the relationship between nodes.
The top node is called the root and is the only node that has no parent. The root in a normal HTML document is the <html/> tag because it has no parent and is the top tag of the document.
Navigating Between Nodes
We can navigate between nodes using these properties:
Here is an example how you can get the parent element of an h1.
var parent = document.getElementById(“heading”).parentNode
If you have found this useful, please consider recommending and sharing it with other fellow developers.
If you have any questions or feedback, let me know in the comments down below.