Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript innerHTML
Next article icon

JavaScript HTML DOM

Last Updated : 28 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The JavaScript HTML DOM (Document Object Model) is a powerful tool that represents the structure of an HTML document as a tree of objects. It allows JavaScript to interact with the structure and content of a webpage. By manipulating the DOM, you can update the content, structure, and styling of a page without requiring a page reload.

What is the JavaScript HTML DOM?

The JavaScript HTML DOM is an interface that allows programs to interact with web pages.

  • It provides a structured way to represent the document’s elements as objects.
  • Using JavaScript, developers can access, modify, or delete HTML elements on the webpage, enabling dynamic content updates.

Features of JavaScript DOM

  • Tree Structure: The DOM is organized like a family tree, with elements that have parent-child relationships. It is easy to find and change things based on their positions.
  • Element Access: You can use different methods like getElementById, getElementsByTagName, and querySelector to access specific elements on a webpage

What is HTML DOM?

  • HTML DOM stands for HTML Document Object Model.
  • It is a programming interface for web documents.
  • It represents the structure of an HTML document as a tree of objects.
  • With the HTML DOM, JavaScript can access and manipulate all elements of an HTML document.

Example: This example shows the accessing the JavaScript HTML DOM by id.

HTML
<html> <head></head>  <body>     <h1 id="demo">This is some text.</h1>     <button onclick="changeText()">       Change Text       </button>      <script>         function changeText() {             let element = document.getElementById("demo");             element.textContent = "Text changed by JavaScript!";         }     </script> </body>  </html> 

In this example

  • The useState hook is used to create a state variable subject and a function setSubject to update its value.
  • The handleInputChange function updates the subject state whenever the user types in the input field.
  • The value entered in the input field is dynamically displayed below it as the state updates.

Output

Output
JavaScript HTML DOM

Accessing Elements in the DOM

getElementById()

Retrieves an element by its id.

let heading = document.getElementById("title"); console.log(heading.textContent);

getElementsByClassName()

Returns a collection of elements with a specified class.

let items = document.getElementsByClassName("list-item"); console.log(items[0].textContent);

getElementsByTagName()

Selects elements by their tag name.

let paragraphs = document.getElementsByTagName("p"); console.log(paragraphs.length);

querySelector()

Returns the first element matching a CSS selector.

let firstParagraph = document.querySelector("p"); console.log(firstParagraph.textContent);

querySelectorAll()

Returns all elements matching a CSS selector.

let allParagraphs = document.querySelectorAll("p"); allParagraphs.forEach(p => console.log(p.textContent));

Modifying the DOM

Changing Content

You can modify the content of an element using textContent or innerHTML.

document.getElementById("title").textContent = "New Heading"; document.getElementById("content").innerHTML = "<b>Updated Content</b>";

Changing Attributes

You can modify attributes like src, href, alt, etc.

document.getElementById("myImage").src = "new-image.jpg";

Adding and Removing Elements

Create an element:

let newPara = document.createElement("p"); newPara.textContent = "This is a new paragraph."; document.body.appendChild(newPara);

Remove an element

let oldPara = document.getElementById("removeMe"); oldPara.remove();

Event Handling in the DOM

JavaScript allows us to handle events such as clicks, keypresses, mouse movements, etc.

Adding an Event Listener

document.getElementById("btn").addEventListener("click", function() {     alert("Button Clicked!"); });

Removing an Event Listener

function sayHello() {     console.log("Hello!"); } let btn = document.getElementById("btn"); btn.addEventListener("click", sayHello); btn.removeEventListener("click", sayHello);

Event Object

The event object provides details about the event.

document.getElementById("inputField").addEventListener("keyup", function(event) {    console.log("Key pressed: ", event.key); }); 

Traversing the DOM

JavaScript provides properties to navigate through the DOM tree.

  • parentNode: Gets the parent element.
  • children: Gets all child elements.
  • firstChild / lastChild: Gets the first/last child.
  • nextSibling / previousSibling: Gets the next/previous sibling.

Example:

let parent = document.getElementById("myDiv").parentNode; console.log(parent.tagName);

Advantages of Using the DOM with JavaScript

  • Interactivity: With the help of the JavaScript and DOM website can be interactive and dyanmic.
  • Real-time Updates: With the help of the JavaScript without refreshing we can update the content of the page, which makes the user experience faster.
  • User Input Handling: The DOM allows us to handle the user inputs in the forms due to which with the backend services there is the seamless communication.

Next Article
JavaScript innerHTML

M

mohd_kashif_ahrari
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • HTML
  • JavaScript-DOM

Similar Reads

  • DHTML JavaScript
    DHTML stands for Dynamic HTML. Dynamic means that the content of the web page can be customized or changed according to user inputs i.e. a page that is interactive with the user. In earlier times, HTML was used to create a static page. It only defined the structure of the content that was displayed
    3 min read
  • HTML JavaScript
    HTML JavaScript is the most popular programming language that helps to add interactivity and provides dynamic behavior. It is known as the client-side scripting language for web pages. JavaScript is used for various purposes, including DOM manipulation, asynchronous requests (AJAX), event handling,
    2 min read
  • HTML | DOM Script Object
    The DOM Script Object is used to represent the HTML <script> element. The script element is accessed by getElementById(). Properties: async: It is used to specify the script is executed asynchronously. charset: It is used to specify the character encoding used in an external script file. defer
    2 min read
  • JavaScript innerHTML
    The innerHTML property in JavScript is used to append the dynamic HTML or text content to an element using JavaScript. It is designed to add dynamic HTML, but developers also use it to add text content as well. It can be directly used with the element by selecting it using DOM manipulation. Syntax:s
    2 min read
  • HTML DOM Subscript Object
    The Subscript Object in HTML DOM is used to represent the HTML <sub> element. The subscript element can be accessed by using the getElementById() method. Syntax: document.getElementById("id") Where id is assigned to the <sub> tag. Example 1: In this example, we will use DOM Subscript Obj
    1 min read
  • HTML DOM Superscript Object
    The superscript object in HTML DOM is used to represent the HTML <sup> element. The superscript element can be accessed by using getElementById(). Syntax: document.getElementById("id") Where id is assigned to the <sup> tag. Example: In this example, we will use DOM Superscript Object C/C
    1 min read
  • HTML DOM Nav Object
    The DOM nav object is used to represent the HTML <nav> element. The<nav> element is accessed by getElementById(). Syntax: document.getElementById("id") Where id is assigned to the <nav> tag. Note: The Nav Object is not supported by Internet Explorer 8 and earlier versions. Example
    1 min read
  • HTML DOM S Object
    The S Object in HTML DOM is used to represent the HTML <s> element. This tag is used to specify that the text content is no longer correct or accurate. The <s> element can be accessed by using the getElementById() method. Syntax: document.getElementById("id"); Where id is assigned to the
    1 min read
  • HTML DOM HR Object
    The DOM HR Object is used to represent the HTML <hr> element. The hr element is accessed by getElementById(). Properties: Align: It is used to set or return the alignment of a horizontal element.color: It is used to set or return the color of the horizontal element.noshade: It is used to set o
    2 min read
  • HTML DOM BR Object
    The DOM BR Object is used to represent the HTML <br> element. The br element is accessed by getElementById(). Syntax: document.getElementById(id) Where "id" is the ID assigned to the br tag. Property: clear: It is used to Sets or return the flow of text around floating objects Example 1: In th
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences