Friday, September 19, 2025
HomeLanguagesJavascriptD3.js selection.nodes() Function

D3.js selection.nodes() Function

The selection.nodes() function in D3.js is used to return the array of selection that contains HTML elements. 

Syntax:

selection.nodes()

Parameters: This function does not accept any parameters.

Return Values: This function returns an array of elements.

Example 1:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" path1tent=
        "width=device-width,initial-scale=1.0">
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
    <script src=
    </script>
</head>
  
<body>
    <div>Some text</div>
    <div>Geeks for neveropen</div>
    <div>Geeks for neveropen</div>
    <div>Some text</div>
  
    <script>
        let selection = d3.selectAll("div")
        console.log(selection.nodes())
    </script>
</body>
  
</html>


Output:

Example 2: When nested containers are given and selection is empty.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" path1tent=
        "width=device-width,initial-scale=1.0">
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
    <script src=
    </script>
</head>
  
<body>
    <div>Some text</div>
    <div>Geeks for <div>neveropen</div>
    </div>
    <div>Geeks <div></div> for neveropen</div>
    <div>Some text</div>
  
    <script>
        let selection = d3.selectAll("div")
        console.log(selection.nodes())
        selection = d3.selectAll("h2")
        console.log(selection.nodes())
    </script>
</body>
  
</html>


Output:

RELATED ARTICLES

Most Popular

Dominic
32301 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11840 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7056 POSTS0 COMMENTS
Thapelo Manthata
6739 POSTS0 COMMENTS
Umr Jansen
6745 POSTS0 COMMENTS