HTML DOM model
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.
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.
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.
We can also get our elements by tag name using the getElementsByClassName() 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.
The HTML DOM allows us to change the content and style of an HTML element by changing its properties.
The innerHTML property can be used to change the content of an HTML element.
document.getElementById(“#header”).innerHTML = “Hello World!”;
In this example we get the element with an id of header and set the inner content to “Hello World!”.
InnerHTML can also be used to put tags in another tag.
document.getElementsByTagName("div").innerHTML = "<h1>Hello World!</h1>"
Here we put a h1 tag into all already existing div.
You can also change the value of an attribute using the DOM.
document.getElementsByTag(“img”).src = “test.jpg”;
In this example we change the src of all tags to test.jpg.
To change the style of an HTML element we need to change the style property of our elements. Here is an example syntax for changing styles:
document.getElementById(id).style.property = new style
Now lets look at an example where we get an element and change the bottom border to a solid black line:
document.getElementsByTag(“h1”).style.borderBottom = “solid 3px #000”;
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.
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 content = document.createTextNode("Hello World!"); div.appendChild(newContent); document.body.insertBefore(div, currentDiv);
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'); elem.parentNode.removeChild(elem);
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" div.parentNode.replaceChild(newDiv, div);
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.
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.
You can define events directly in your HTML code using attributes on your tags. Here is an example of an onclick event:
<h1 onclick=”this.innerHTML = ‘Hello!’”>Click me!</h1>
In this example, the text of the
<h1 onclick=”changeText(this)”>Click me!</h1>
document.getElementById(“btn”).onclick = changeText();
Now let’s look at how you can assign event listeners to your HTML elements.
Here we just assigned a clickevent that calls the runEvent method when our btn element is clicked.
You can also assign multiple events to a single element:
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 tag because it has no parent and is the top tag of the document.
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.
*Originally published by Gabriel Tanner at *https://medium.freecodecamp.org
DOM stands for Document Object Model. From MDN: “The Document Object Model is an API for HTML and XML documents. It provides a structural representation of the document, enabling the developer to modify its content and visual presentation. Essentially, it connects web pages to scripts or programming languages.”
Does your business need an interactive website or app?
The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.
For more inquiry click here https://bit.ly/31eZyDZ
Book Free Interview: https://bit.ly/3dDShFg
With 15+ years of domain expertise, we have successfully delivered 13800+ projects and have successfully garnered 6800+ happy customers with 97%+ client retention rate.
The compound assignment operators consist of a binary operator and the simple assignment operator.
The binary operators, work with two operands. For example a+b where + is the operator and the a, b are operands. Simple assignment operator is used to assign values to a variable(s).
It’s quite common to modify values stored in variables. To make this process a little quicker, we use compound assignment operators.
You can also check my video tutorial compound assignment operators.
Let’s consider an example. Suppose price = 5 and we want to add ten more to it.
var price = 5;
price = price + 10;
We added ten to price. Look at the repetitive price variable. We could easily use a compound += to reduce this. We do this instead.
price += 5;
Awesome. Isn’t it? What’s the value of price now? Practice and comment below. If you don’t know how to practice check these lessons.
Lets bring down the price by 5 again and display it.
We use console.log command to display what is stored in the variable. It is very help for debugging.
Debugging let’s you find errors or bugs in your code. More on this later.
price -= 5;
Lets multiply price and show it.
and finally we will divide it.
If you have any doubts, comment below.