MyInternships.in

HTML DOM

HTML DOM Complete Reference

This reference collects the most commonly used properties, methods, and collections of the document object in one place. Use it as a quick lookup while working with the DOM.


About the document Object

The document object is the root of the DOM and the entry point for all page manipulation in JavaScript. It exposes properties that describe the page, collections that group related elements, and methods that find, create, and modify nodes.

The table below lists the members you will reach for most often. Names ending in () are methods you call; the rest are properties or collections you read.

Common Document Properties

NameDescription
activeElementReturns the element that currently has focus
baseURIReturns the absolute base URL of the document
bodyReturns the <body> element
characterSetReturns the character encoding of the document
charsetDeprecated alias for characterSet
cookieReturns or sets the document's cookies
defaultViewReturns the window object of the document
designModeGets or sets whether the document is editable
doctypeReturns the Document Type Declaration node
documentElementReturns the root <html> element
documentURIReturns the location of the document as a string
domainReturns the domain name of the document server
fullscreenElementReturns the element currently shown in fullscreen
headReturns the <head> element
hiddenReturns true if the document is not visible
lastModifiedReturns the date and time the document was last modified
readyStateReturns the loading status of the document
referrerReturns the URL of the page that linked to this one
titleReturns or sets the document title
URLReturns the full URL of the document
visibilityStateReturns the visibility state of the document

Common Document Collections

NameDescription
anchorsReturns all <a> elements that have a name attribute
embedsReturns all <embed> elements
formsReturns all <form> elements
imagesReturns all <img> elements
linksReturns all <a> and <area> elements with an href
scriptsReturns all <script> elements
styleSheetsReturns all stylesheets attached to the document
childrenReturns the element's child elements

Common Document Methods

NameDescription
getElementById()Returns the element with the given id
getElementsByClassName()Returns elements with the given class name
getElementsByTagName()Returns elements with the given tag name
getElementsByName()Returns elements with the given name attribute
querySelector()Returns the first element matching a CSS selector
querySelectorAll()Returns all elements matching a CSS selector
createElement()Creates a new element node
createAttribute()Creates a new attribute node
createTextNode()Creates a new text node
createComment()Creates a new comment node
appendChild()Adds a new child node to an element
removeChild()Removes a child node from an element
replaceChild()Replaces a child node with another
insertBefore()Inserts a node before an existing child
cloneNode()Creates a copy of a node
open()Opens an output stream for writing
write()Writes HTML or text to the document stream
writeln()Writes text plus a newline to the stream
close()Closes a stream opened with open()
hasFocus()Returns true if the document has focus
addEventListener()Attaches an event handler to the document
removeEventListener()Removes an event handler from the document
getElementsByName()Returns elements sharing a name attribute
createDocumentFragment()Creates an empty document fragment
importNode()Imports a node from another document
adoptNode()Moves a node from another document into this one
normalize()Merges adjacent text nodes and removes empty ones
elementFromPoint()Returns the element at given coordinates

Example

This runnable page uses several of the members from the tables above - title, images, forms, getElementById, and createElement - and reports what it found.

Using several document members together
Example
<!DOCTYPE html>
<html>
<body>
  <h1>DOM reference demo</h1>
  <img src="a.png" alt=""><img src="b.png" alt="">
  <form></form>
  <button onclick="report()">Inspect the document</button>
  <div id="out" style="margin-top:12px;"></div>

  <script>
    function report() {
      const lines = [
        "Title: " + document.title,
        "Images: " + document.images.length,
        "Forms: " + document.forms.length,
        "URL: " + document.URL
      ];
      const out = document.getElementById("out");
      out.innerHTML = "";
      lines.forEach(function (t) {
        const p = document.createElement("p");
        p.textContent = t;
        out.appendChild(p);
      });
    }
  </script>
</body>
</html>
💡

Collections such as document.images and document.forms are live: they update automatically as the page changes, so re-reading length always gives the current count.

Key Takeaways

  • The document object is the root entry point of the DOM.
  • Properties describe the page; collections group related elements; methods find and build nodes.
  • Names ending in () are methods you call.
  • Live collections update automatically as the DOM changes.

Related HTML Topics

Keep learning with these closely related tutorials.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships