What is JavaScript HTML DOM Document? Detail with examples and use cases.

The Document Object

When an HTML document is loaded into a web browser, it becomes a document object.

The document object is the root node of the HTML document.

The document object is a property of the window object.

The document object is accessed with:

window.document or just document

The HTML DOM (Document Object Model)

When a web page is loaded, the browser creates a Document Object Model of the page.

With the object model, JavaScript gets all the power it needs to create dynamic HTML.

What is the HTML DOM?

  • The HTML elements as objects
  • The properties of all HTML elements
  • The methods to access all HTML elements
  • The events for all HTML elements

The Objects are organized in a hierarchy. This hierarchical structure applies to the organization of objects in a Web document.

  • Window object − Top of the hierarchy. It is the outmost element of the object hierarchy.
  • Document object − Each HTML document that gets loaded into a window becomes a document object. The document contains the contents of the page.
  • Form object − Everything enclosed in the <form>…</form> tags sets the form object.
  • Form control elements − The form object contains all the elements defined for that object such as text fields, buttons, radio buttons, and checkboxes.

Example

getElementById()

The following example changes the content (the innerHTML) of the <p> element with id="demo" :-

<html>
<body>

<p id=”demo”></p>

<script>
document.getElementById(“demo”).innerHTML = “Hello Roshan!”;
</script>

</body>
</html>

In the example above, getElementById is a method, while innerHTML is a property.

document.write()

<!DOCTYPE html>
<html>
<body>

<script>
document.write(Date());
</script>

</body>
</html>

document.querySelector()

<!DOCTYPE html>
<html>
<body>

<p>Add a background color to the first element with class=”example”: </p>

<script>
document.querySelector(“p”).style.backgroundColor = “red”;
</script>

</body>
</html>

The open() and close() Methods

<!DOCTYPE html>
<html>
<body>

<p>Add a background color to the first element with class=”example”: </p>

<button onclick = “myfunction()”>click me</button>

<script>
function myfuction() {

document.open();
document.write (“<h1> Hello Roshan </h1>”);

document.close();

}
</script>

</body>
</html>

Related Posts

Enterprise Delivery Pipelines: Technical Strategies for DevOps Training China

Introduction Software development teams frequently experience severe delivery bottlenecks when handoffs between developers and operations engineers rely on manual interventions. Code that runs reliably on a developer’s…

Read More

Inside the Overseas Work Visa for Indians: A Relocation Advisor’s Strategic Playbook

Indian professionals looking to build an international career quickly discover that finding an overseas job is only half the battle. Securing the legal right to work in…

Read More

The Vital Role of Digital Inclusion Programs in Advancing Modern Social Welfare

Introduction When local governments and public institutions digitize vital civic resources—from municipal court filings and welfare applications to public housing portals—they often assume the broader public can…

Read More

Best Weekend Events in Delhi: How to Find and Plan Outings

Introduction Being a student or an early-career creator in the capital means having endless curiosity but operating within realistic financial boundaries. You want to immerse yourself in…

Read More

Top DataOps Pipeline Testing Tools for Modern Data Teams

Introduction Modern data pipelines are complex distributed systems. A standard production workflow connects transactional databases, third-party APIs, object storage, streaming queues, transformation layers, cloud data warehouses, and…

Read More

Accelerating Pipeline Root Cause Analysis Using Telemetry and Graph Intelligence

Modern enterprise data architectures rely on an intricate web of batch jobs, streaming brokers, cloud warehouses, and SaaS APIs where standard task monitoring often falls short—a pipeline…

Read More