document.getElementsByTagName("*") Or document.all

Learn document.getelementsbytagname("*") or document.all with practical examples, diagrams, and best practices. Covers javascript, google-chrome, firefox development techniques with visual explanat...

Navigating DOM Element Selection: document.getElementsByTagName("*") vs. document.all

Hero image for document.getElementsByTagName("*") Or document.all

Explore the historical context, browser compatibility, and modern best practices for selecting all elements in the DOM using JavaScript, focusing on document.getElementsByTagName("*") and the deprecated document.all.

When working with the Document Object Model (DOM) in JavaScript, developers often need to select multiple elements. Two historical methods for selecting all elements on a page are document.getElementsByTagName("*") and document.all. While both aim to achieve a similar outcome, their origins, browser support, and current relevance differ significantly. Understanding these differences is crucial for writing robust, cross-browser compatible, and future-proof web applications.

The document.all Collection: A Legacy Feature

The document.all collection is a proprietary feature introduced by Microsoft in Internet Explorer 4.0. It was designed to provide a convenient way to access all elements in the document. While it gained some traction due to IE's dominance, it was never part of any official W3C DOM standard. Modern browsers, including Google Chrome and Firefox, have implemented document.all primarily for compatibility with older websites, but it is officially deprecated and its use is strongly discouraged.

if (document.all) {
  console.log("This browser supports document.all (likely for compatibility).");
  // Avoid using document.all for new development
  // let allElements = document.all;
  // console.log(`Total elements via document.all: ${allElements.length}`);
} else {
  console.log("document.all is not directly supported or is deprecated.");
}

Checking for document.all support (for historical context only).

The Standard Approach: document.getElementsByTagName("*")

document.getElementsByTagName("*") is the standard, W3C-compliant method for retrieving a live HTMLCollection of all elements within the document. The asterisk * acts as a wildcard, matching any element tag name. This method returns a live collection, meaning that if elements are added or removed from the DOM after the collection is retrieved, the collection will automatically update to reflect these changes.

let allElements = document.getElementsByTagName("*");
console.log(`Total elements via getElementsByTagName("*"): ${allElements.length}`);

// Example: Iterate through all elements
for (let i = 0; i < allElements.length; i++) {
  // console.log(allElements[i].tagName);
}

// Demonstrating live collection behavior
let newDiv = document.createElement('div');
document.body.appendChild(newDiv);
console.log(`Total elements after adding a div: ${allElements.length}`); // Length will increase

Using document.getElementsByTagName("*") to get all elements and observe live collection behavior.

flowchart TD
    A[Start] --> B{"Need to select all DOM elements?"}
    B -- Yes --> C{Is cross-browser compatibility critical?}
    C -- Yes --> D["Use document.getElementsByTagName("*")"]
    C -- No --> E{Is it a legacy IE-only application?}
    E -- Yes --> F["Consider document.all (with caution)"]
    E -- No --> G["Use modern selectors (e.g., querySelectorAll)"]
    D --> H[Process elements]
    F --> H
    G --> H
    H --> I[End]

Decision flow for selecting all DOM elements.

Modern Alternatives: querySelectorAll

While document.getElementsByTagName("*") is a standard and reliable method, for many modern use cases, document.querySelectorAll("*") offers a more flexible and often preferred alternative. querySelectorAll returns a static NodeList, which means it does not update automatically if the DOM changes. This can be an advantage in scenarios where you need a snapshot of the DOM at a specific moment. It also supports CSS selectors, making it incredibly powerful for more specific selections.

let allElementsStatic = document.querySelectorAll("*");
console.log(`Total elements via querySelectorAll("*"): ${allElementsStatic.length}`);

// Demonstrating static NodeList behavior
let anotherDiv = document.createElement('div');
document.body.appendChild(anotherDiv);
console.log(`Total elements via querySelectorAll("*") after adding a div: ${allElementsStatic.length}`); // Length will NOT change

// Convert NodeList to Array for easier manipulation if needed
let allElementsArray = Array.from(allElementsStatic);
console.log(`Converted to Array: ${allElementsArray.length}`);

Using document.querySelectorAll("*") and observing its static nature.