Open In App

D3.js selector() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The d3.selector() function is used to return a function that returns the very first descendant of the element given as the parameter.

Syntax:

 d3.selector(selector)

Parameters: This function takes only one parameter which is given above and described below:

  • selector: This is the string of the element that is to be selected.

Return Values: This function returns a boolean value.

Below given are a few examples of the function given above.

Example 1:




<!DOCTYPE html> 
<html lang="en"
<head
    <meta charset="UTF-8"
    <meta name="viewport"
            path1tent="width=device-width, 
                    initial-scale=1.0"> 
    <title>Document</title
</head
<style>
    p{
        line-height:5px;
    }
</style
<body>  
    <div>
  
    </div>
    <div>
        <p>This is para1</p>
        <p>This is para2</p>
        <p>This is para3</p>
        <p>This is para4</p>
    </div>
  <script src
  </script>
  <script>
      // Parent of div is HTML 
    var div = d3.select("div").select(d3.selector("div"));
    console.log(div);
  </script
</body
</html>


Output:

Example 2:




<!DOCTYPE html> 
<html lang="en"
<head
    <meta charset="UTF-8"
    <meta name="viewport"
            path1tent="width=device-width, 
                    initial-scale=1.0"> 
    <title>Document</title
</head
<style>
    p{
        line-height:5px;
    }
</style
<body>  
    <div>
        <p>This is para1
              
        <p>This is para2</p>
          
        <p>This is para3</p>
        </p>
    </div>
  <script src
  </script>
  <script>
      // First descendant of Div 
    var para = d3.select("div").select(d3.selector("p"));
    console.log(para);
      
      // First descendant of Div 
    var p = d3.select("div").select("p");
    console.log(p);
  
    console.log("Both p and para are equal");
    console.log(para.node());
    console.log(p.node());
  
  </script
</body
</html>


Output:



Last Updated : 19 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads