Saturday, September 6, 2025
HomeLanguagesJavascriptD3.js selection.node() Function

D3.js selection.node() Function

The selection.node() function in D3.js is used to return the first element in the selection. If the selection does not contain any elements then it returns null. 

Syntax:

selection.node()

Parameters: This function does not accept any parameters.

Return Values: This function returns the first element in the selection.

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>
    <h2>Some text</h2>
    <div>Geeks for Geeks </div>
  
    <script>
        let selection = d3.selectAll("div")
        let div = selection.node();
        console.log(div)
  
        // Printing innerHTML of the tag
        console.log(div.innerHTML)
        selection = d3.selectAll("h1")
          
        // Null is returned
        console.log(selection.node())
    </script>
</body>
  
</html>


Output:

Example 2:

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.node())
        selection = d3.selectAll("h2")
  
        // Null is returned
        console.log(selection.node())
    </script>
</body>
  
</html>


Output:

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS