DOM in JavaScript
DOM stands for Document Object Model. It is a programming interface that allows JavaScript to access and manipulate HTML documents.
When a web page is loaded in the browser, the browser creates a DOM representation of the HTML page. JavaScript can use the DOM to select, modify, add, or remove HTML elements.
For example, consider the following HTML:
<h1 id="title">Hello World</h1>
Using JavaScript, we can access and change this element.
document.getElementById("title").textContent = "Welcome to JavaScript";
After executing the JavaScript code, the content of the <h1> element changes from:
Hello World
to:
Welcome to JavaScript
DOM Tree
The browser represents an HTML document as a tree of objects called the DOM Tree.
In the DOM, every HTML element is treated as an object called a node.
JavaScript can use these nodes to interact with the web page.
Selecting HTML Elements
JavaScript provides several methods to select elements.
1. getElementById()
The getElementById() method is used to select an element using its id.
<h1 id="title">Hello</h1>
let element = document.getElementById("title");
console.log(element);
2. getElementsByClassName()
The getElementsByClassName() method is used to select elements using a class name.
<p class="message">Hello</p>
<p class="message">Welcome</p>
let elements = document.getElementsByClassName("message");
console.log(elements);
This method returns a collection of elements because multiple HTML elements can have the same class name.
3. getElementsByTagName()
The getElementsByTagName() method is used to select elements using their tag name.
<p>First paragraph</p>
<p>Second paragraph</p>
let elements = document.getElementsByTagName("p");
console.log(elements);
This method returns a collection of all <p> elements.
4. querySelector()
The querySelector() method selects the first element that matches a CSS selector.
<h1 class="title">JavaScript DOM</h1>
let element = document.querySelector(".title");
console.log(element);
Here, .title is a CSS class selector.
We can also select elements using an id selector.
document.querySelector("#title");
Or using a tag selector.
document.querySelector("h1");
5. querySelectorAll()
The querySelectorAll() method selects all elements that match a CSS selector.
<p class="message">Hello</p>
<p class="message">Welcome</p>
let elements = document.querySelectorAll(".message");
console.log(elements);
This method returns a collection of all elements with the class "message".
Changing Text Content
The textContent property is used to change or access the text inside an HTML element.
For example:
Changing HTML Content
The innerHTML property is used to access or change the HTML content inside an element.
<div id="content"></div>
document.getElementById("content").innerHTML =
"<h2>Welcome</h2>";
This adds an <h2> element inside the <div>.
The result in the browser will be:
Welcome
Changing CSS Styles
JavaScript can also change the CSS styles of an HTML element using the style property.
Creating a New Element
The document.createElement() method is used to create a new HTML element.
For example:
let heading = document.createElement("h2");
heading.textContent = "New Heading";
document.body.appendChild(heading);
Here, JavaScript creates a new <h2> element and adds it to the <body>.
Removing an Element
The remove() method is used to remove an HTML element.
Adding an Event
The DOM also allows JavaScript to respond to user actions such as clicking a button.
For example:
<button id="btn">Click Me</button>
<h1 id="message"></h1>
let button = document.getElementById("btn");
button.addEventListener("click", function() {
document.getElementById("message").textContent =
"Button Clicked";
});
When the user clicks the button, the text "Button Clicked" appears on the web page.
Common DOM Methods
| Method | Description |
|---|---|
getElementById() |
Selects an element using its ID |
getElementsByClassName() |
Selects elements using a class name |
getElementsByTagName() |
Selects elements using a tag name |
querySelector() |
Selects the first matching element |
querySelectorAll() |
Selects all matching elements |
createElement() |
Creates a new HTML element |
appendChild() |
Adds an element to another element |
remove() |
Removes an element |
Common DOM Properties
| Property | Description |
|---|---|
textContent |
Changes or accesses text content |
innerHTML |
Changes or accesses HTML content |
style |
Changes CSS styles |
value |
Gets or changes the value of an input element |
Conclusion
The DOM allows JavaScript to interact with HTML elements on a web page. Using the DOM, we can select elements, change text and HTML content, modify CSS styles, create new elements, remove existing elements, and respond to user actions.
Understanding the DOM is important because it helps us create dynamic and interactive web pages using JavaScript.




Top comments (0)