JavaScript Document Object and the DOM

The JavaScript document object represents the web page loaded in the current browser tab. It is the main entry point to the Document Object Model (DOM), so JavaScript can find elements, read or change their content and attributes, create new elements, and respond to what happens on the page.

const heading = document.querySelector("h1");
heading.textContent = "Updated with JavaScript";

In this example, document.querySelector() finds the first <h1> element and textContent replaces its text. Changes made through the DOM affect the current page in memory; they do not rewrite the original server file.

Common pattern: find an element, read or change it, then attach behavior when needed.

const message = document.getElementById("message");
message.textContent = "Welcome to plus2net";
Table of Contents

Document Object and DOM Top ↑

The DOM is a tree representation of the HTML document. Elements such as headings, paragraphs, links, forms, and images become nodes that JavaScript can work with. The global document object exposes the methods and properties used to access that tree.

const title = document.title;
const url = document.URL;
const links = document.links.length;

console.log(title, url, links);

The window object represents the browser window or tab. The document object is a property of that window and represents the page inside it. In normal browser JavaScript, document and window.document refer to the same document.

Finding Elements in the Document Top ↑

JavaScript provides several ways to locate elements. Use the method that best matches the structure of the page.

MethodWhat it returnsTypical use
getElementById()One element or nullFind a unique element by its id.
getElementsByName()A NodeListFind elements sharing the same name, especially form controls.
getElementsByTagName()A live HTMLCollectionFind all elements with a tag such as p or img.
querySelector()First matching element or nullUse a CSS selector to find one element.
querySelectorAll()A static NodeListUse a CSS selector to find multiple elements.
const firstCard = document.querySelector(".card");
const allCards = document.querySelectorAll(".card");

allCards.forEach((card) => {
  card.classList.add("border");
});

Working with an element by ID is covered separately in the forms section.

Creating and Inserting Elements Top ↑

document.createElement() creates an element object, but the new element is not visible until it is inserted into the DOM.

const note = document.createElement("p");
note.textContent = "This paragraph was created with JavaScript.";
document.body.append(note);

For plain text, prefer textContent. Use innerHTML only when you intentionally need to insert HTML and the content is trusted or safely sanitized.

Useful Document Properties Top ↑

Property / methodPurpose
document.titleRead or change the page title.
document.URLRead the complete URL of the current document.
document.linksAccess links in the document.
document.anchorsLegacy collection of anchors that have a name attribute.
document.domainRead the current document domain; changing it is deprecated and should not be used in new code.
document.lastModifiedBrowser-provided string describing the document's last modification time.
document.referrerRead the referring document URL when the browser makes it available.
document.cookieRead or write cookies that are available to JavaScript.

Document Events and Focus Top ↑

The document can also receive events. For example, DOMContentLoaded runs after the HTML has been parsed, and document.hasFocus() reports whether the document currently has focus.

document.addEventListener("DOMContentLoaded", () => {
  console.log("The DOM is ready.");
});

Document Tutorial Reference Top ↑

TopicWhat it covers
createElement()Create and insert elements dynamically.
getElementsByName()Work with multiple elements that share a name.
getElementsByTagName()Find elements by HTML tag name.
Document titleRead and update the page title.
Document URLRead the current page URL.
lastModifiedRead the browser-reported last modified value.
domainCurrent document domain and legacy behavior.
document.write()Legacy document-writing method and safer modern alternatives.
linksAccess link elements in the document.
anchorsAccess named anchors.
hasFocus()Check whether the document has focus.

Common Mistakes Top ↑

  • Running code before the element exists: place the script after the HTML it uses or wait for DOMContentLoaded.
  • Assuming a lookup always succeeds: methods such as getElementById() and querySelector() can return null.
  • Using innerHTML for plain text: use textContent when no HTML is needed.
  • Using old document collections for new code: modern selectors are usually clearer, although older methods remain useful and widely supported.
JavaScript


Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer