0% found this document useful (0 votes)
1K views

Javascript Info Basic DOM Node-Properties

Uploaded by

Mark Dipad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
1K views

Javascript Info Basic DOM Node-Properties

Uploaded by

Mark Dipad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
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 Event Target Node = t = Fleeent cas fi sveelenere TIMLELenene FvinputELenent | [ HTMLBodyEtenent | | HiWAnchos€lenent input Eype Zou a ee ‘The classes are: + EventTarget - is the root “abstract” class. 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 concrete node classes that inherit from it, namely: Text for text nodes, Element for element nodes and more exotic ones like Comment. for comment nodes. + Element ~is a base class for DOM elements, it provides element-level navigation like nextElementSibling, children and searching methods like getElementsByTagName, querySelector . A browser supports not only HTML, but also XML and SVG. The Element class serves as a base for more specific classes: SVGElement , XMLElement and HTMLELement . + HTMLElement ~ is finally the basic class for all HTML elements. Itis inherited by concrete HTML elements: + HTMLInputElement - the class for elements, + HTMLBodyElement ~ the class for elements, + HTMLAnchorélement — the class for elements, + .and so on, There are many other tags with their own classes that may specific properties and methods, while some elements, such as ,
,
do not have any specific properties, so they are instances of HTMLElenent class So, the full set of properties and methods of a given node comes as the result of the inheritance. For example, let's consider the DOM object for an 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, + HINLELement — it provides common HTML element methods (and getters/setters), + Element — provides generic element methods, + Node — provides common DOM node properties, + EventTarget ~ gives the support for events (to be covered), + and finally it inherits from Object , so “plain object” methods like hasOwnProperty are also available. To see the DOM node class name, we can recall that an object usually has the constructor property. It references the class constructor, and constructor. name is its name: alert( document.body.constructor.name ); // HTMLBodyElement Or we can just toString it: alert( document.body ); // [object HTMLBodyElement] We also can use instanceof to check the inheritance: alert( document.body instanceof HTMLBodyElement ); // true alert( document body instanceof HTMLElement ); // true alert( document.body instanceof Element ); // true alert( document.body instanceof Node ); // true alert( document.body instanceof EventTarget ); // true ‘As we can see, DOM nodes are regular JavaScript objects. They use prototype-based classes for inheritance. That's also easy to see by outputting an element with console.dir(elem) in a browser. There in the console you can see HTMLElement.prototype, Element.prototype and so on. @ console.dir(elem) versus console.1log(elem) Most browsers support two commands in their developer tools: console.log and console.dir . They output their arguments to the console. For JavaScript objects these commands usually do the same. But for DOM elements they are different: + console. og(elem) shows the element DOM tree + console.dir(elem) shows the element as a DOM object, good to explore its properties. Try iton document. body © wwein the spec In the specification, DOM classes aren't described by using JavaScript, but a special Interface description language (IDL), that is usually easy to understand. In IDL all properties are prepended with their types. For instance, DOMString, boolean and so on, Here's an excerpt from it, with comments: // Define HTMLInputElement 71 The colon ":" means that HTMLInputElement inherits from HTMLElement interface HTMLInputElement: HTMLElement { // here go properties and methods of elements // “DoMString" means that the value of a property is a string attribute DoMString accept; attribute DoMString alt; attribute DOMString autocomplete; attribute DoMString value; // boolean value property (true/false) attribute boolean autofocus; // now the method: "void" means that the method returns no value void select(); 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: 1 for element nodes, + elem.nodeType + elem.nodeType == 3 for text nodes, + elem.nodeType == 9 for the document object, + there are few other values in the specification, For instance: In modem 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: alert( document .body.nodeName ); // BODY alert( document.body.tagName ); // BODY 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: “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 For instance:
Headline!
‘As we can see, only text is returned, as if all were cut out, but the text in them remained, In practice, reading such text is rarely needed. Writing to textContent is much more useful, because i llows to write text the “safe way”. Let's say we have an arbitrary string, for instance entered by a user, and want to show it. With innerHTML we'll have it inserted “as HTML’, with all HTML tags. With textContent well have it inserted “as text, all symbols are treated literally. Compare the two:
4. The first
gets the name “as HTML": all tags become tags, so we see the bold name. 2 The second
gets the name “as text”, so we literally see Winnie-the-Pooh! In most cases, we expect the text from a user, and want to treat it as text. We don't want unexpected HTML in our site. An assignment to textContent does exactly that. The “ The *hidden' attribute and the DOM property specifies whether the element is visible or not. \den” property ‘We can use it in HTML or assign it using JavaScript, like this:
JavaScript assigned the property "hidden"
Technically, hidden works the same as style="display:none” . But it’s shorter to write Here's a blinking element
A blinking element
felem.hidden, 1000); More properties DOM elements also have additional properties, in particular those that depend on the class: + value -the value for ,