What is HTML DOM

The HTML DOM is an Object Model for HTML. It defines

The HTML DOM (Document Object Model)

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

The HTML DOM model is constructed as a tree of Objects:

The HTML DOM Tree of Objects

DOM HTML tree

Finding HTML Elements

When you want to access HTML elements with JavaScript, you have to find the elements first.

There are a couple of ways to do this:

  • Finding HTML elements by id
  • Finding HTML elements by tag name
  • Finding HTML elements by class name
  • Finding HTML elements by CSS selectors
  • Finding HTML elements by HTML object collections
  • Finding HTML Element by Id

    The easiest way to find an HTML element in the DOM, is by using the element id.

    This example finds the element with id="intro":

    Example

    var myElement = document.getElementById("intro");

    If the element is found, the method will return the element as an object (in myElement).

    If the element is not found, myElement will contain null.

    Finding HTML Elements by Tag Name

    This example finds all <p> elements:

    Example

    var x = document.getElementsByTagName("p");

    This example finds the element with id="main", and then finds all <p> elements inside "main":

    Example

    var x = document.getElementById("main");
    var y = x.getElementsByTagName("p");

    Finding HTML Elements by Class Name

    If you want to find all HTML elements with the same class name, use getElementsByClassName().

    This example returns a list of all elements with class="intro".

    Example

    var x = document.getElementsByClassName("intro");

    Finding elements by class name does not work in Internet Explorer 8 and earlier versions.

    Finding HTML Elements by CSS Selectors

    If you want to find all HTML elements that matches a specified CSS selector (id, class names, types, attributes, values of attributes, etc), use the querySelectorAll() method.

    This example returns a list of all <p> elements with class="intro".

    Example

    var x = document.querySelectorAll("p.intro");

    The querySelectorAll() method does not work in Internet Explorer 8 and earlier versions.

    Finding HTML Elements by HTML Object Collections

    HTML object collections are also accessible:

    HTML DOM Tutorial

    Full HTMLDOM Tutorial

    This has been a short introduction to HTMLDOM.

    For a full HTMLDOM tutorial go to w3resource HTMLDOM Tutorial.