Wednesday, December 10, 2025
HomeLanguagesJavascriptHTML DOM NodeIterator previousNode() Method

HTML DOM NodeIterator previousNode() Method

The NodeIterator.previousNode() method sets the NodeIterator to the previous node in the document and returns the previous node. This method returns null when there are no nodes left in the set.

Syntax:

node = nodeIterator.previousNode();

Parameters: This method takes no parameter.

Return Value: This method returns the previous node in the set.

Example: In this example, we will create a node iterator and hence will iterate backward using the previousNode() method.




<!DOCTYPE HTML> 
<html>  
<head>
    <meta charset="UTF-8">
    <title>HTML | DOM previousNode() Method</title>
</head>   
  
<body style="text-align:center;">
    <h1 style="color:green;">  
     neveropen
    </h1
    <p
HTML | DOM previousNode() Method
    </p>
  
    <button onclick = "Geeks()">
    Click Here
    </button>
    <p id="a"></p>
    <script
        var a = document.getElementById("a");
        function Geeks(){
           const nodeIterator = document.createNodeIterator(
                document.body,
                NodeFilter.SHOW_ELEMENT
                )
             
            let nextNode=nodeIterator.nextNode();
            nextNode=nodeIterator.nextNode();
            var prevNode=nodeIterator.previousNode();
            a.innerHTML =
'Previous Node content is : '+prevNode.textContent;
            console.log(prevNode);
}
</script>
</body>
</html>


Output:

Before Clicking Button:

After Clicking Button:

In the console: in Console, the previous node can be seen.

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Safari
  • Opera
  • Internet Explorer
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32435 POSTS0 COMMENTS
Milvus
104 POSTS0 COMMENTS
Nango Kala
6807 POSTS0 COMMENTS
Nicole Veronica
11947 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12021 POSTS0 COMMENTS
Shaida Kate Naidoo
6940 POSTS0 COMMENTS
Ted Musemwa
7192 POSTS0 COMMENTS
Thapelo Manthata
6885 POSTS0 COMMENTS
Umr Jansen
6878 POSTS0 COMMENTS