WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript HTML DOM Navigation


With the HTML DOM, you can explore the hub tree utilizing hub relationships.


DOM Nodes

According to the W3C HTML DOM standard, everything in a HTML report is a node:

  • The whole report is an archive node
  • Every HTML component is a component node
  • The message inside HTML components are content nodes
  • Every HTML property is a characteristic node
  • All remarks are remark nodes
DOM HTML tree

With the HTML DOM, all hubs in the hub tree can be gotten to by JavaScript.

New hubs can be made, what not hubs can be adjusted or erased.


Node Relationships

The hubs in the hub tree have a progressive relationship to each other.

The terms parent, tyke, and kin are utilized to depict the connections.

  • In a hub tree, the best hub is known as the root (or root node)
  • Every hub has precisely one parent, aside from the root (which has no parent)
  • A hub can have various children
  • Siblings (siblings or sisters) are hubs with the equivalent parent
<html>

  <head>
      <title>DOM Tutorial</title>
  </head>

  <body>
      <h1>DOM Lesson one</h1>
      <p>Hello world!</p>
  </body>

</html>
Node tree

From the HTML above you can read:

  • <html> is the root node
  • <html> has no guardians
  • <html> is the parent of <head> and <body>
  • <head> is the primary offspring of <html>
  • <body> is the last offspring of <html>

and:

  • <head> has one tyke: <title>
  • <title> has one tyke (a content hub): "DOM Tutorial"
  • <body> has two youngsters: <h1> and <p>
  • <h1> has one tyke: "DOM Lesson one"
  • <p> has one tyke: "Hello world!"
  • <h1> and <p> are siblings

Navigating Between Nodes

You can utilize the accompanying hub properties to explore between hubs with JavaScript:

  • parentNode
  • childNodes[nodenumber]
  • firstChild
  • lastChild
  • nextSibling
  • previousSibling

Warning !

A basic mistake in DOM preparing is to anticipate that a component hub should contain content.

In this model: <title>DOM Tutorial</title>, the component hub <title> does not contain content. It contains a text node with the esteem "DOM Tutorial".

The estimation of the content hub can be gotten to by the hub's innerHTML property, or the nodeValue.


Child Nodes and Node Values

In expansion to the innerHTML property, you can likewise utilize the childNodes and nodeValue properties to get the substance of an element.

The following precedent gathers the hub estimation of a <h1> component and duplicates it into a <p> element:

Example

<html>
<body>

<h1 id="intro">My First Page</h1>

<p id="demo">Hello!</p>

<script>
var myText = document.getElementById("intro").childNodes[0].nodeValue;
document.getElementById("demo").innerHTML = myText;
</script>

</body>
</html>

In the precedent above, getElementById is a strategy, while childNodes and nodeValue are properties.

In this instructional exercise we utilize the innerHTML property. Be that as it may, learning the technique above is valuable for understanding the tree structure and the route of the DOM.

Using the firstChild property is equivalent to utilizing childNodes[0]:

Example

<html>
<body>

<h1 id="intro">My First Page</h1>

<p id="demo">Hello World!</p>

<script>
myText = document.getElementById("intro").firstChild.nodeValue;
document.getElementById("demo").innerHTML = myText;
</script>

</body>
</html>
Try it Yourself »

DOM Root Nodes

There are two exceptional properties that enable access to the full document:

  • document.body - The body of the document
  • document.documentElement - The full document

Example

<html>
<body>

<p>Hello World!</p>
<div>
<p>The DOM is very useful!</p>
<p>This precedent exhibits the <b>document.body</b> property.</p>
</div>

<script>
alert(document.body.innerHTML);
</script>

</body>
</html>
Try it Yourself »

Example

<html>
<body>

<p>Hello World!</p>
<div>
<p>The DOM is very useful!</p>
<p>This precedent exhibits the <b>document.documentElement</b> property.</p>
</div>

<script>
alert(document.documentElement.innerHTML);
</script>

</body>
</html>
Try it Yourself »

The nodeName Property

The nodeName property determines the name of a node.

  • nodeName is perused only
  • nodeName of a component hub is equivalent to the tag name
  • nodeName of a characteristic hub is the quality name
  • nodeName of a content hub is dependably #text
  • nodeName of the record hub is dependably #document

Note: nodeName dependably contains the capitalized label name of a HTML element.


The nodeValue Property

The nodeValue property determines the estimation of a node.

  • nodeValue for component hubs is undefined
  • nodeValue for content hubs is the content itself
  • nodeValue for trait hubs is the property value

The nodeType Property

The nodeType property restores the sort of hub. nodeType is perused only.

The most imperative hub types are:

Element type NodeType
Element 1
Attribute 2
Text 3
Comment 8
Document 9