Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

D3.js selection.empty() Function

  • Last Updated : 14 Aug, 2020

The d3.empty() function in D3.js is used to return a boolean value. The value is true when the selection contains no elements and it returns false when the selection is non-empty.

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.empty();

Parameters: This function takes no parameters.

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
div{
    background-color: green;
    margin-bottom: 5px;
    padding: 10px;
    width: fit-content;
}
</style
<body>  
    <div>Some text</div>
    <div>Geeks for geeks</div>
    <div>Geeks for geeks</div>
    <div>Some text</div>
  <script src
  </script>
  <script src=
</script>
  <script>
      let selection=d3.selectAll("div")
      console.log(selection.empty())
  </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
div{
    background-color: green;
    margin-bottom: 5px;
    padding: 10px;
    width: fit-content;
}
</style
<body>  
    <div>Some text</div>
    <div>Geeks for geeks</div>
    <div>Geeks for geeks</div>
    <div>Some text</div>
  <script src
  </script>
  <script src=
</script>
  <script>
    // Selection is empty because no
    // container name h2 is rendered.
      let selection=d3.selectAll("h2")
      console.log(selection.empty())
  </script
</body
</html>

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!