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:
How to select DOM Elements in JavaScript ?
Next article icon

How are DOM utilized in JavaScript ?

Last Updated : 08 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

What is DOM

The document object Model (DOM) represents the full HTML document. When an HTML document is loaded within the browser, it becomes a document object. The root element represents the HTML document, its properties, and its methods. With the assistance of a document object, we will add dynamic content to our web page. 

In another word, the way a document's content is accessed and modified is the Document Object Model or DOM. The objects are organized in a hierarchy. Web document objects are applied in this hierarchical data structure.

  • Window object: It is at the top of the hierarchy. It's the utmost element of the object hierarchy.
  • Document Object: Every HTML document becomes a Document object when it is loaded in windows. Contents of the page reside in the documents.
  • Form object: The shape object enclosed within the <form>...</form> tags 
  • Form control elements: All the elements defined for that object such as text fields, buttons, radio buttons, and checkboxes, are contained in the shape object.

DOM and JavaScript

The DOM is not a programming language, but without it, the JavaScript language wouldn't have any model or notion of websites, HTML documents, SVG documents, and their parts. The document as an entire, the head, tables within the document, table headers, text within the table cells, and every other element in a document is part of the document object model for that document. They will all be accessed and manipulated using the DOM and a scripting language like JavaScript.

Using DOM, JavaScript can perform multiple tasks. It can create new elements and attributes, change the prevailing elements and attributes and even remove existing elements and attributes. JavaScript also can react to existing events and create new events on the page.

Example 1: The getElementById() using id is used to access elements and attributes. The innerHTML is used to access the content of an element.

HTML
<!DOCTYPE html> <html>  <head>     <title>DOM by javascript</title> </head>  <body>     <h1 style="color:green">GeeksforGeeks</h1>     <h1 id="one">using Javascript</h1>     <p>This is the welcome message.</p>     <script type="text/javascript">         var text = document.getElementById("one").innerHTML;         console.log("The first heading is " + text);     </script> </body>  </html> 

Output:

 

Example 2: The getElementsByTagName() method will return an array of all the items with the same tag name. Elements and attributes are accessed using the tag name.

HTML
<!DOCTYPE html> <html>  <head>     <title>DOM using javascript</title> </head>  <body>     <h1 style="color:green">GeeksforGeeks</h1>     <h1>Welcome</h1>     <p>Hi GeeksforGeeks Welcomes you</p>     <h2>GFG</h2>     <p id="second">A computer science portal</p>     <script type="text/javascript">         var paragraphs = document.getElementsByTagName("p");                 console.log("Content in the second paragraph is "                                               + paragraphs[1].innerHTML);                 document.getElementById("second").innerHTML =                          "The original message is changed.";     </script> </body>  </html> 

Output:

 

Next Article
How to select DOM Elements in JavaScript ?

G

guptavivek0503
Improve
Article Tags :
  • Technical Scripter
  • JavaScript
  • Web Technologies
  • Technical Scripter 2022
  • HTML-DOM
  • JavaScript-Questions

Similar Reads

  • How to run JavaScript in Chrome Browser ?
    Javascript is a highly flexible scripting language that enables developers to incorporate interactive elements into web pages. Web development could not be imagined without it as one of the most widely used methods to implement JavaScript is via web browsers. One of the very popular web browsers is
    5 min read
  • How to Add JavaScript in HTML Document?
    To add JavaScript in HTML document, several methods can be used. These methods include embedding JavaScript directly within the HTML file or linking an external JavaScript file. Inline JavaScriptYou can write JavaScript code directly inside the HTML element using the onclick, onmouseover, or other e
    4 min read
  • How to trigger events in JavaScript ?
    JavaScript is a high-level, interpreted, dynamically typed client-side scripting language. While HTML is static and defines the structure of a web page, JavaScript adds interactivity and functionality to HTML elements. This interaction is facilitated through events, which are actions or occurrences
    2 min read
  • How to select DOM Elements in JavaScript ?
    Selecting DOM (Document Object Model) elements is a fundamental aspect of web development with JavaScript. It allows developers to interact with and manipulate elements on a webpage dynamically. Proper selection of elements is crucial for tasks such as updating content, adding event listeners, or mo
    3 min read
  • How to Access XML Data via JavaScript ?
    XML stands for Extensible Markup Language. It is a popular format for storing and exchanging data on the web. It provides a structured way to represent data that is both human-readable and machine-readable. There are various approaches to accessing XML data using JavaScript which are as follows: Tab
    2 min read
  • How to Handle JavaScript Events in HTML ?
    An Event is an action or occurrence recognized by the software. It can be triggered by the user or the system. Mostly Events are used on buttons, hyperlinks, hovers, page loading, etc. All this stuff gets into action(processed) with the help of event handlers. In this article, you will learn about d
    3 min read
  • How to check a JavaScript Object is a DOM Object ?
    Checking if a JavaScript object is a DOM object involves verifying whether the object represents an element or component within the Document Object Model (DOM) of an HTML or XML document. This can be done by checking if the object is an instance of Node, Element, or other specific DOM interfaces. Wh
    2 min read
  • How to call JavaScript function in HTML ?
    In HTML, you can easily call JavaScript functions using event attributes like onclick and onload. Just reference the function name within these attributes to trigger it. You can also call functions directly within script blocks using standard JavaScript syntax. Let's create an HTML structure with so
    2 min read
  • How to Select an Element by ID in JavaScript ?
    In JavaScript, we can use the "id" attribute to interact with HTML elements. The "id" attribute in HTML assigns a unique identifier to an element. This uniqueness makes it easy for JavaScript to precisely target and manipulate specific elements on a webpage. Selecting elements by ID helps in dynamic
    2 min read
  • How to Display Images in JavaScript ?
    To display images in JavaScript, we have different approaches. In this article, we are going to learn how to display images in JavaScript. Below are the approaches to display images in JavaScript: Table of Content Using CreateElementUsing InnerHTMLApproach 1: Using CreateElementIn an HTML document,
    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