GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — JavaScript - DOM Navigation
Selected Reading
JavaScript - DOM NavigationIn JavaScript, with HTML DOM we can navigate the tree nodes using the relationship between the nodes. Each HTML element is represented as a node in the DOM tree. The HTML document object is represented as root node. There are different types of nodes such as root node, parent, child and sibling nodes. The relationship between these nodes help to navigate the DOM tree. What are DOM Nodes?When a webpage gets loaded in the browser, it creates a document object. The 'document' object is the root of the web page, and you can access other HTML nodes of the web page. In the HTML DOM, everything is a node.
You can access all nodes of the HTML DOM. Relationship between HTML DOM NodesIn the HTML DOM, each node has a relationship with other nodes. Each node is present in the hierarchical structure in the DOM tree. Here are the terms which we will use in this chapter.
Let's understand the relationship between nodes in the below example. Example<html>
<head>
<title> JavaScrip - DOM Navigation </title>
</head>
<body>
<div>
<h3> Hi Users! </h3>
<p> Hello World! </p>
</div>
</body>
</html>
In the above program,
Navigating Between Nodes Using JavaScriptNavigating between nodes means finding the parent, child, sibling, etc. element of a particular element in the DOM tree. You can use the below methods and properties to navigate between nodes in the DOM tree.
Below, we will use each method to navigate through the DOM elements. Accessing the First Child ElementYou can access the particular child element using the firstChild or firstElementChild property. SyntaxFollow the syntax below to use the 'firstChild' and 'firstElementChild' properties to access the first child element. element.firstChild; element.firstChildElement; ExampleIn the below code, <div> element contains the text followed by three <p> elements. When we use the 'firstChild' property, it returns the text node containing the 'Numbers' text, and when we use the 'firstChildElement' property, it returns the first element. <!DOCTYPE html>
<html>
<body>
<div id = "num">Numbers
<p> One </p>
<p> Two </p>
<p> Three </p>
</div>
<div id = "demo"> </div>
<script>
const output = document.getElementById('demo');
const numbers = document.getElementById('num');
// Using the firstChild property
output.innerHTML += "numbers.firstChild: " +
numbers.firstChild.textContent.trim() + "<br>";
// Using the firstElementChild property
output.innerHTML += "numbers.firstElementChild: " + numbers.firstElementChild.textContent + "<br>";
</script>
</body>
</html>
Accessing the Last Child ElementYou can use the lastChild or lastChildElement properties to access the last child of the particular HTML node. SyntaxFollow the syntax below to use the 'lastChild' and 'laststElementChild' properties to access the las=st child element. element.lastChild; element.lastChildElement; ExampleIn the below code, we have defined the <div> element containing 3 <p> elements containing the name of the programming languages. In the output, you can see that the lastElementChild property returns the last <p> element, and the lastChild property returns the text node of the div element. <!DOCTYPE html>
<html>
<body>
<div id = "lang">
<p> Java </p>
<p> JavaScript </p>
<p> HTML </p>
Programming Languages
</div>
<div id = "demo"> </div>
<script>
const output = document.getElementById('demo');
const langs = document.getElementById('lang');
// Using the lastChild property
output.innerHTML += "langs.lastChild: " + langs.lastChild.textContent.trim() + "<br>";
// Using the lastElementChild property
output.innerHTML += "langs.lastElementChild: " + langs.lastElementChild.textContent + "<br>";
</script>
</body>
</html>
Accessing all children of HTML ElementYou can use the childNodes property to access the node list of all children elements or the children property to access the HTML collection of all children. SyntaxFollow the syntax below to access all children elements of the DOM element. element.children; element.childNodes; ExampleIn the below code, we use the childNodes property to access all child nodes of the <div> element. In the output, you can see that it also returns the text nodes with undefined text as it contains the text nodes after and before each HTML element node. <!DOCTYPE html>
<html>
<body>
<div id = "lang">
<p> Java </p>
<p> JavaScript </p>
<p> HTML </p>
programming Languages
</div>
<div id = "output"> </div>
<script>
let output = document.getElementById('output');
let langs = document.getElementById('lang');
output.innerHTML += "All children of the div element are - " + "<br>";
let allChild = langs.childNodes;
for (let i = 0; i < allChild.length; i++) {
output.innerHTML += allChild[i].nodeName + " " + allChild[i].innerHTML + "<br>";
}
</script>
</body>
</html>
Accessing the Parent Node of HTML ElementYou can use the parentNode property to access the parent node of the particular HTML node. SyntaxFollow the syntax below to use the parentNode property. element.parentNode; ExampleIn the below code, we access the <li> element having d equal to 'blue' in JavaScript. After that, we use the parentNode property to access the parent node. It returns the 'UL' node, which we can observe in the output. <!DOCTYPE html>
<html>
<body>
<ul id = "color">
<li id = "blue"> Blue </li>
<li> Pink </li>
<li> Red </li>
</ul>
<div id = "output">The child node of the color list is: </div>
<script>
const blue = document.getElementById('blue');
document.getElementById('output').innerHTML += blue.parentNode.nodeName;
</script>
</body>
</html>
Accessing the Next Sibling NodeThe nextSibling property is used to access the next sibling. SyntaxFollow the syntax below to use the nextSibling property. element.nextSibling ExampleIn the below code, we have access to the <li> element with an id equal to 'apple', and access to the next sibling node using the nextSibling property. It returns the <li> node having the id equal to 'banana'. <!DOCTYPE html>
<html>
<body>
<ul id = "fruit">
<li id = "apple"> Apple </li>
<li id = "banana"> Banana </li>
<li id = "watermealon"> Watermealon </li>
</ul>
<div id = "output">The next sibling node of the apple node is: </div>
<script>
const apple = document.getElementById('apple');
document.getElementById('output').innerHTML += apple.nextElementSibling.textContent;
</script>
</body>
</html>
Accessing the Previous Sibling NodeThe previousSibling property is used to access the previous sibling node from the DOM tree. SyntaxFollow the syntax below to use the previousSibling property. element.previousSibling; ExampleIn the below code, we access the previous sibling of the <li> element with an id equal to 'banana. It returns the <li> element having id equal to 'apple'. <!DOCTYPE html>
<html>
<body>
<ul id = "fruit">
<li id = "apple"> Apple </li>
<li id = "banana"> Banana </li>
<li id = "watermealon"> Watermealon </li>
</ul>
<div id = "output">The previous sibling node of the banana node is: </div>
<script>
const banana = document.getElementById('banana');
document.getElementById('output').innerHTML += banana.previousElementSibling.textContent;
</script>
</body>
</html>
DOM Root NodesThe HTML DOM contains two root nodes.
Example: Using the document.body<!DOCTYPE html>
<html>
<body>
<div> This is demo! </div>
<div id="output"> </div>
<script>
const output = document.getElementById('output');
output.innerHTML += "The body of the document is: " + document.body.innerHTML;
</script>
</body>
</html>
Example: Using the document.documentElement<!DOCTYPE html>
<html>
<body>
<h1> Hi, Users! </h1>
<div id="output"> </div>
<script>
const output = document.getElementById('output');
output.innerHTML += "The full document is " + document.documentElement.innerHTML;
</script>
</body>
</html>
DOM nodeName PropertyThe nodeName property of the HTML DOM element is used to get the name of the node, and it has specifications given below.
SyntaxFollow the syntax below to use the nodeName property to get the name of the node. element.nodeName; ExampleIn the below code, we access the <div> element and use the nodeName property. It returns the tag name in the uppercase. <!DOCTYPE html>
<html>
<body>
<div id = "output"> </div>
<script>
const output = document.getElementById('output');
output.innerHTML = "The node name of the div node is: " + output.nodeName;
</script>
</body>
</html>
DOM nodeValue PropertyThe nodeValue is used to get the value of the, and it has specifications given below.
SyntaxFollow the syntax below to use the nodeValue property to get the value of the node. element.nodeValue; ExampleThe <div> element contains some text, and the <p> element in the below code. The first child element of the <div> element is the text node, and the second child node of the <div> element is the <p> element. In the output, you can see that when you use the nodeValue property with the text node, it returns the text. Otherwise, it returns null when you use it with the HTML element node. <!DOCTYPE html>
<html>
<body>
<div id = "num">
Numbers
<p> One </p>
</div>
<div id = "output"> </div>
<script>
const output = document.getElementById('output');
const num = document.getElementById('num');
let child = num.childNodes;
output.innerHTML += "The value of the first child is: " + child[0].nodeValue + "<br>";
output.innerHTML += "The value of the second child is: " + child[1].nodeValue + "<br>";
</script>
</body>
</html>
Types of Node in DOMHere, we have given different node types in the HTML DOM.
DOM nodeType PropertyThe nodeType property returns the type of the node as shown in the above table. SyntaxFollow the syntax below to use the nodeType property to get the type of the node. element.nodeType; ExampleIn the below code, we use the nodeType property to get the type of the node. <!DOCTYPE html>
<html>
<body>
<div id = "output"> </div>
<script>
const output = document.getElementById('output');
output.innerHTML += "The type of the div node is: " + output.nodeType;
</script>
</body>
</html>
Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |