Skip to content
Related Articles

Related Articles

Improve Article

D3.js selector() Function

  • Last Updated : 19 Aug, 2020

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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :