Let’s get a more in-depth look at DOM nodes.
In this chapter we’ll see more into what they are and learn their most used properties.
DOM node classes
Different DOM nodes may have different properties. For instance, an element node corresponding to tag <a> has link-related properties, and the one corresponding to <input> has input-related properties and so on. Text nodes are not the same as element nodes. But there are also common properties and methods between all of them, because all classes of DOM nodes form a single hierarchy.
Each DOM node belongs to the corresponding built-in class.
The root of the hierarchy is EventTarget, that is inherited by Node, and other DOM nodes inherit from it.
Here’s the picture, explanations to follow:
The classes are:
-
EventTarget – is the root “abstract” class for everything.
Objects of that class are never created. It serves as a base, so that all DOM nodes support so-called “events”, we’ll study them later.
-
Node – is also an “abstract” class, serving as a base for DOM nodes.
It provides the core tree functionality:
parentNode,nextSibling,childNodesand so on (they are getters). Objects ofNodeclass are never created. But there are other classes that inherit from it (and so inherit theNodefunctionality). -
Document, for historical reasons often inherited by
HTMLDocument(though the latest spec doesn’t dictate it) – is a document as a whole.The
documentglobal object belongs exactly to this class. It serves as an entry point to the DOM. -
CharacterData – an “abstract” class, inherited by:
-
Element – is the base class for DOM elements.
It provides element-level navigation like
nextElementSibling,childrenand searching methods likegetElementsByTagName,querySelector.A browser supports not only HTML, but also XML and SVG. So the
Elementclass serves as a base for more specific classes:SVGElement,XMLElement(we don’t need them here) andHTMLElement. -
Finally, HTMLElement is the basic class for all HTML elements. We’ll work with it most of the time.
It is inherited by concrete HTML elements:
- HTMLInputElement – the class for
<input>elements, - HTMLBodyElement – the class for
<body>elements, - HTMLAnchorElement – the class for
<a>elements, - …and so on.
- HTMLInputElement – the class for
There are many other tags with their own classes that may have specific properties and methods, while some elements, such as <span>, <section>, <article> do not have any specific properties, so they are instances of HTMLElement class.
So, the full set of properties and methods of a given node comes as the result of the chain of inheritance.
For example, let’s consider the DOM object for an <input> element. It belongs to HTMLInputElement class.
It gets properties and methods as a superposition of (listed in inheritance order):
HTMLInputElement– this class provides input-specific properties,HTMLElement– it provides common HTML element methods (and getters/setters),Element– provides generic element methods,