What is DOM in JavaScript?

#javascript #web-development

What is GEEK

Buddha Community

Felix Kling


The Javascript DOM (Document Object Model) is an interface that allows developers to manipulate the content, structure and style of a website. In this article, we will learn what the DOM is and how you can manipulate it using Javascript. This article can also be used as a reference for the basic DOM operations.

What is the DOM?

At the most basic level, a website consists of an HTML and CSS document. The browser creates a representation of the document known as Document Object Model (DOM). This document enables Javascript to access and manipulate the elements and styles of a website. The model is built in a tree structure of objects and defines:

  • HTML elements as objects
  • Properties and events of the HTML elements
  • Methods to access the HTML elements

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).

DOM Document

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

Now that we understand what the DOM document is we can start getting our first HTML elements. There are many different ways to do so using the Javascript DOM here are the most common:

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 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.

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.

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.

Changing a value of an attribute

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.

Changing the style

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.

Adding and deleting elements

Now we will take a look at how we can add new elements and delete existing ones.

Adding elements

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!"); 

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.

Deleting elements

var elem = document.querySelector('#header');

Here we get an element and delete it using the removeChild() method.

Replace elements

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.

Writing directly into the HTML output stream

We can also write HTML expressions and JavaScript directly into the HTML output stream using the write() method.

document.write(“<h1>Hello World!</h1><p>This is a paragraph!</p>”);

We can also pass JavaScript expressions like a date object.


The write() method can also take multiple arguments that will be appended to the document in order of their occurrence.

Event Handlers

The HTML DOM also allows Javascript to react to HTML events. Here I’ve just listed some of the most important ones:

  • HTML elements as objects
  • Properties and events of the HTML elements
  • Methods to access the HTML elements

Assign Events

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

will change to “Hello!” when you click the button.You can also call functions when an event is triggered as you can see in the next example.

<h1 onclick=”changeText(this)”>Click me!</h1>

Here we call the changeText() method when the button is clicked and pass the element as an attribute.

We can also assign the same events in our Javascript code.

document.getElementById(“btn”).onclick = changeText();

Assign Events Listeners

Now let’s look at how you can assign event listeners to your HTML elements.

document.getElementById(“btn”)addEventListener('click', runEvent);

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:

document.getElementById(“btn”)addEventListener('mouseover', runEvent);

Node Relationships

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.

Navigating Between Nodes

We can navigate between nodes using these properties:

  • HTML elements as objects
  • Properties and events of the HTML elements
  • Methods to access the HTML elements

Here is an example how you can get the parent element of an h1.

var parent = document.getElementById(“heading”).parentNode


You made it all the way until the end! Hope that this article helped you understand the Javascript DOM and how to use it to manipulate elements on your website.

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.

Learn More

The Complete JavaScript Course 2019: Build Real Projects!

Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

JavaScript Bootcamp - Build Real World Applications

The Web Developer Bootcamp

ES5 to ESNext — here’s every feature added to JavaScript since 2015

JavaScript Vulnerabilities

Understanding Closures in JavaScript

Move Over GUI, Lets Build a VUI With JavaScript

Learn JavaScript - JavaScript Course for Beginners

3 Ways to Set Default Value in JavaScript

*Originally published by Gabriel Tanner at *https://medium.freecodecamp.org

Angular Minds


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.”

In CSS it is used for selectors behind the scenes. The DOM is more important to directly know about when you’re working with javascript, unless you’re using abstraction libraries like jQuery

Rahul Jangid


What is JavaScript - Stackfindover - Blog

Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.

Who invented JavaScript?

JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.

What is JavaScript?

JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.

Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.

JavaScript Hello World Program

In JavaScript, ‘document.write‘ is used to represent a string on a browser.

<script type="text/javascript">
	document.write("Hello World!");

How to comment JavaScript code?

  • For single line comment in JavaScript we have to use // (double slashes)
  • For multiple line comments we have to use / * – – * /
<script type="text/javascript">

//single line comment

/* document.write("Hello"); */


Advantages and Disadvantages of JavaScript

#javascript #javascript code #javascript hello world #what is javascript #who invented javascript

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

It is said that a digital resource a business has must be interactive in nature, so the website or the business app should be interactive. How do you make the app interactive? With the use of JavaScript.

Does your business need an interactive website or app?

Hire Dedicated JavaScript Developer from WebClues Infotech as the developer we offer is highly skilled and expert in what they do. Our developers are collaborative in nature and work with complete transparency with the customers.

The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.

Get your business app with JavaScript

For more inquiry click here https://bit.ly/31eZyDZ

Book Free Interview: https://bit.ly/3dDShFg

#hire dedicated javascript developers #hire javascript developers #top javascript developers for hire #hire javascript developer #hire a freelancer for javascript developer #hire the best javascript developers

Niraj Kafle


The essential JavaScript concepts that you should understand

As a JavaScript developer of any level, you need to understand its foundational concepts and some of the new ideas that help us developing code. In this article, we are going to review 16 basic concepts. So without further ado, let’s get to it.

#javascript-interview #javascript-development #javascript-fundamental #javascript #javascript-tips

Ajay Kapoor


JS Development Company India | JavaScript Development Services

PixelCrayons: Our JavaScript web development service offers you a feature-packed & dynamic web application that effectively caters to your business challenges and provide you the best RoI. Our JavaScript web development company works on all major frameworks & libraries like Angular, React, Nodejs, Vue.js, to name a few.

With 15+ years of domain expertise, we have successfully delivered 13800+ projects and have successfully garnered 6800+ happy customers with 97%+ client retention rate.

Looking for professional JavaScript web app development services? We provide custom JavaScript development services applying latest version frameworks and libraries to propel businesses to the next level. Our well-defined and manageable JS development processes are balanced between cost, time and quality along with clear communication.

Our JavaScript development companies offers you strict NDA, 100% money back guarantee and agile/DevOps approach.

#javascript development company #javascript development services #javascript web development #javascript development #javascript web development services #javascript web development company

Santosh J


JavaScript compound assignment operators

JavaScript is unarguablly one of the most common things you’ll learn when you start programming for the web. Here’s a small post on JavaScript compound assignment operators and how we use them.

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.

They are:

  • +=
  • -+
  • *=
  • /=

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.

price *=5;

and finally we will divide it.

price /=5;

If you have any doubts, comment below.

#javascript #javascript compound assignment operators #javascript binary operators #javascript simple assignment operator #doers javascript