Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

D3.js selection.size() Function

  • Last Updated : 19 Aug, 2020

The selection.size() function in D3.js is used to calculate and return the size of the selection. It counts the total element in the selection and returns its size.

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

selection.size();

Parameters: This function does not accept any parameters.

Return Value: This function returns a Numerical value.



Example 1: When selection is non-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=
    </script>
    <script src=
    </script>
</head>
  
<body>
    <div>Geeks for Geeks </div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  
    <script>
        let selection = d3.selectAll("div")
        let div = selection.size();
        console.log(div)
    </script>
</body>
  
</html>


Output:
5

Example 2: When 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=
    </script>
    <script src=
    </script>
</head>
  
<body>
    <script>
        let selection=d3.selectAll("h2")
        let size=selection.size();
        console.log(size)
      </script
</body>
  
</html>

Output:

0



My Personal Notes arrow_drop_up
Recommended Articles
Page :