Node properties: type, tag and contents
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 has link-related properties, and the one corresponding to 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, childNodes and so on (they are getters). Objects of Node class are never created. But there are other classes that inherit from it (and so inherit the Node functionality).
- Document, for historical reasons often inherited by HTMLDocument (though the latest spec doesn’t dictate it) – is a document as a whole.
The document global object belongs exactly to this class. It serves as an entry point to the DOM.
- CharacterData – an “abstract” class, inherited by:
- Text – the class corresponding to a text inside elements, e.g. Hello in Hello
The “nodeType” property
The nodeType property provides one more, “old-fashioned” way to get the “type” of a DOM node. It has a numeric value: - elem.nodeType == 1 for element nodes, - elem.nodeType == 3 for text nodes, - elem.nodeType == 9 for the document object, - there are few other values in the specification. For instance: In modern scripts, we can use instanceof and other class-based tests to see the node type, but sometimes nodeType may be simpler. We can only read nodeType, not change it.
Tag: nodeName and tagName
Given a DOM node, we can read its tag name from nodeName or tagName properties: For instance: Is there any difference between tagName and nodeName? Sure, the difference is reflected in their names, but is indeed a bit subtle. - The tagName property exists only for Element nodes. - The nodeName is defined for any Node: - for elements it means the same as tagName. - for other node types (text, comment, etc.) it has a string with the node type. In other words, tagName is only supported by element nodes (as it originates from Element class), while nodeName can say something about other node types. For instance, let’s compare tagName and nodeName for the document and a comment node: If we only deal with elements, then we can use both tagName and nodeName - there’s no difference.
innerHTML: the contents
The innerHTML property allows to get the HTML inside the element as a string. We can also modify it. So it’s one of the most powerful ways to change the page. The example shows the contents of document.body and then replaces it completely: We can try to insert invalid HTML, the browser will fix our errors:
Beware: “innerHTML+=” does a full overwrite
We can append HTML to an element by using elem.innerHTML+=“more html”. Like this: But we should be very careful about doing it, because what’s going on is not an addition, but a full overwrite. Technically, these two lines do the same: In other words, innerHTML+= does this: 1. The old contents is removed. 2. The new innerHTML is written instead (a concatenation of the old and the new one). As the content is “zeroed-out” and rewritten from the scratch, all images and other resources will be reloaded. In the chatDiv example above the line chatDiv.innerHTML+=“How goes?” re-creates the HTML content and reloads smile.gif (hope it’s cached). If chatDiv has a lot of other text and images, then the reload becomes clearly visible. There are other side-effects as well. For instance, if the existing text was selected with the mouse, then most browsers will remove the selection upon rewriting innerHTML. And if there was an with a text entered by the visitor, then the text will be removed. And so on. Luckily, there are other ways to add HTML besides innerHTML, and we’ll study them soon.
outerHTML: full HTML of the element
The outerHTML property contains the full HTML of the element. That’s like innerHTML plus the element itself. Here’s an example: Beware: unlike innerHTML, writing to outerHTML does not change the element. Instead, it replaces it in the DOM. Yeah, sounds strange, and strange it is, that’s why we make a separate note about it here. Take a look. Consider the example: Looks really odd, right? In the line () we replaced div with
A new element
. In the outer document (the DOM) we can see the new content instead of theA new element
was inserted in its place. - div still has its old value. The new HTML wasn’t saved to any variable. It’s so easy to make an error here: modify div.outerHTML and then continue to work with div as if it had the new content in it. But it doesn’t. Such thing is correct for innerHTML, but not for outerHTML. We can write to elem.outerHTML, but should keep in mind that it doesn’t change the element we’re writing to (‘elem’). It puts the new HTML in its place instead. We can get references to the new elements by querying the DOM.nodeValue/data: text node content
The innerHTML property is only valid for element nodes. Other node types, such as text nodes, have their counterpart: nodeValue and data properties. These two are almost the same for practical use, there are only minor specification differences. So we’ll use data, because it’s shorter. An example of reading the content of a text node and a comment: For text nodes we can imagine a reason to read or modify them, but why comments? Sometimes developers embed information or template instructions into HTML in them, like this: …Then JavaScript can read it from data property and process embedded instructions.
textContent: pure text
The textContent provides access to the text inside the element: only text, minus all The “hidden” attribute and the DOM property specifies whether the element is visible or not.
We can use it in HTML or assign it using JavaScript, like this:
Technically, hidden works the same as style=“display:none”. But it’s shorter to write.
Here’s a blinking element: DOM elements also have additional properties, in particular those that depend on the class:
- value – the value for , Follow the lesson from Microsoft Web-Dev-For-Beginners courseThe “hidden” property
More properties
Summary
alert( document.body.constructor.name ); // HTMLBodyElement