D3.js | d3.select() Function

The d3.select() function in D3.js is used to select the first element that matches the specified selector string. If any element is not matched then it returns the empty selection. If multiple elements are matched with the selector then only the first matching element will be selected.

Syntax:

d3.select("element")

Parameters: This function accepts single parameter which holds the element name.



Return Value: This function returns the selected elements.

Below programs illustrate the d3.select() function in D3.js:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        D3.js | d3.select() Function
    </title>
      
    <script src = "https://d3js.org/d3.v4.min.js"></script>
</head>
  
<body>
      
    <div>GeeksforGeeks</div>
      
    <script>
      
        // Calling the select() function
        var a = d3.select("div").text();
          
        // Getting the selected element
        console.log(a);
    </script>
</body>
  
</html>                    

chevron_right


Output:

GeeksforGeeks 

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
         
    <title>
        D3.js | d3.select() Function
    </title>
      
    <script src="https://d3js.org/d3.v4.min.js"></script>
</head>
   
<body>
    <p>GeeksforGeeks</p>
         
    <p>A computer science portal for geeks</p>
         
    <script>
         
        // Calling the select() function
        var a = d3.select("p").text();
            
        // Getting the selected element
        console.log(a);
    </script>
</body>
     
</html>

chevron_right


Output:

GeeksforGeeks

Reference: https://devdocs.io/d3~5/d3-selection#select



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.