-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
The JavaScript DOM Document is the entry point to everything you do with the Document Object Model. When a web page loads in the browser, the browser creates a DOM tree that represents the HTML structure of that page. The document object represents the entire HTML document and allows JavaScript to access, modify, create, and remove elements dynamically. Almost every interaction with a web page using JavaScript starts with the document object.
In this tutorial, you will learn what the JavaScript DOM document is, how it works, commonly used document methods and properties, practical examples, common mistakes, best practices, and real-world use cases.
The DOM (Document Object Model) is a programming interface for HTML and XML documents. It represents the page as a structured tree of nodes, where each element, attribute, and text is treated as an object.
The document object is the root of this tree. Through it, JavaScript can:
Read the content of a web page
Change HTML elements and attributes
Change CSS styles dynamically
Add or remove elements
Respond to user actions
Simply put, if JavaScript needs to interact with the web page, it does so through the document.
The document object is important because it allows developers to:
Access HTML elements programmatically
Build dynamic and interactive user interfaces
Update content without reloading the page
Validate and process user input
Create modern web applications
Without the document object, JavaScript would have no way to understand or control what is displayed on the screen.
Consider this simple HTML:
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello World</h1>
<p>Welcome to JavaScript</p>
</body>
</html>
The DOM represents this as a tree structure:
document
html
head
title
body
h1
p
Each part of the HTML becomes a node, and JavaScript can navigate this tree using the document object.
The document object is automatically available in the browser. You do not need to create it.
console.log(document);
This logs the entire document object, including all elements and methods.
Gets or sets the title of the web page.
console.log(document.title);
document.title = "JavaScript DOM Tutorial";
Returns the full URL of the current page.
console.log(document.URL);
Returns the domain name of the server.
console.log(document.domain);
Provides access to the <body> element.
document.body.style.backgroundColor = "lightyellow";
Provides access to the <head> element.
console.log(document.head);
One of the most common uses of the document object is selecting HTML elements.
Selects an element by its unique ID.
<p id="message">Hello</p>
let msg = document.getElementById("message");
msg.textContent = "Hello Aarushi";
Selects all elements with a given class name.
<p class="info">One</p>
<p class="info">Two</p>
let items = document.getElementsByClassName("info");
console.log(items.length);
Selects elements by tag name.
let paragraphs = document.getElementsByTagName("p");
console.log(paragraphs.length);
Selects the first element that matches a CSS selector.
let heading = document.querySelector("h1");
heading.style.color = "blue";
Selects all elements that match a CSS selector.
let listItems = document.querySelectorAll(".info");
listItems.forEach(item => {
item.style.fontWeight = "bold";
});
The document object allows you to create new elements dynamically.
let newPara = document.createElement("p");
newPara.textContent = "This is a new paragraph";
document.body.appendChild(newPara);
let heading = document.querySelector("h1");
document.body.insertBefore(newPara, heading);
let parent = document.body;
let element = document.getElementById("message");
parent.removeChild(element);
let para = document.querySelector("p");
para.remove();
document.getElementById("content").innerHTML = "<strong>Hello Priya</strong>";
document.getElementById("content").textContent = "Hello Priya";
textContent is safer because it does not parse HTML.
The document object can listen to user interactions.
document.addEventListener("click", function() {
console.log("Document clicked");
});
This ensures the DOM is fully loaded before accessing elements.
document.addEventListener("DOMContentLoaded", function() {
console.log("DOM is ready");
});
document.getElementById("darkBtn").addEventListener("click", function() {
document.body.style.backgroundColor = "black";
document.body.style.color = "white";
});
let ul = document.createElement("ul");
["Aarushi", "Priya", "Saanvi"].forEach(name => {
let li = document.createElement("li");
li.textContent = name;
ul.appendChild(li);
});
document.body.appendChild(ul);
document.getElementById("myForm").addEventListener("submit", function(e) {
let name = document.getElementById("username").value;
if (name === "") {
e.preventDefault();
alert("Name is required");
}
});
Accessing elements before the DOM is fully loaded
Using innerHTML unnecessarily, leading to security issues
Forgetting that getElementsByClassName returns a live collection
Not checking if an element exists before using it
Mixing up querySelector and querySelectorAll
Use DOMContentLoaded or place scripts at the bottom of the page
Prefer textContent over innerHTML when possible
Cache DOM selections for better performance
Keep DOM manipulation minimal inside loops
Use clear and meaningful element IDs and classes
Building interactive dashboards
Creating dynamic forms and validations
Updating content without page reloads
Implementing theme switches and UI effects
Developing single-page applications
The JavaScript DOM document object is the foundation of all web page manipulation. It represents the entire HTML document and provides powerful methods and properties to access, modify, create, and remove elements dynamically. By understanding how the document object works, how to select elements, and how to manipulate the DOM efficiently, developers can build responsive, interactive, and user-friendly web applications. Mastering the document object is a crucial step in becoming confident with JavaScript and front-end development.
Write JavaScript to log the current page title and URL using the Document object.
How would you dynamically change the page title to "My DOM Document Practice"?
Using the Document object, change the background color of the <body> element to light gray.
How can you log the domain of the current webpage using JavaScript?
Using document.head, add a new <meta> element with name="author" and content="Your Name".
Write code to count all <form> elements on the page and display the total number in the console.
Access the first form on the page and set the value of the input field named "username" to "John Doe".
How would you change the target attribute of the first link (<a>) on the page to _blank?
Write JavaScript to update the alt attribute of the first image on the page to "Updated Image".
Using document.createElement and document.createTextNode, create a new paragraph and append it to the <body>.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
