HomeLanguagesJavascriptD3.js selection.remove() Function

D3.js selection.remove() Function

The selection.remove() function is used to remove the selected elements from the document and return a new selection with the removed elements. Also, new selection are now detached from the DOM

Syntax:

selection.remove();

Parameters: This function does not accept any parameters.

Return Values: This function returns a new selection.

Example 1:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" path1tent=
        "width=device-width, initial-scale=1.0">
  
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
      
    <style>
        h1 {
            color: green;
        }
  
        div {
            width: 300px;
            color: #ffffff;
            height: 50px;
            background-color: green;
            margin: 10px;
        }
    </style>
  
<body>
    <h1>neveropen</h1>
  
    <h4>D3.js selection.remove() Function</h4>
  
    <p>The divs will be removed.</p>
  
    <div><span>1. This div will be removed.</span></div>
    <div><span>2. This div will be removed.</span></div>
  
    <button>Click Here!</button>
  
    <script>
        function func() {
            // Selecting  div and
            // Removing the div
            var div = d3.selectAll("div")
                .remove();
        }
        btn = document.querySelector("button");
        btn.addEventListener("click", func);
  
    </script>
</body>
  
</html>


Output:

Before clicking the “Click Here!” element:

After clicking the “Click Here!” element:

Example 2:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" path1tent=
        "width=device-width, initial-scale=1.0">
  
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
  
    <style>
        h1 {
            color: green;
        }
  
        div {
            width: 300px;
            color: #ffffff;
            height: 50px;
            background-color: green;
            margin: 10px;
        }
    </style>
  
<body>
    <h1>Geeks for neveropen</h1>
  
    <h4>D3.js selection.remove() Function</h4>
  
    <p>The <span> from div will be removed.</p>
  
    <div><span>1. This text will be removed.</span></div>
    <div><span>2. This div will not be removed.</span></div>
  
    <button>Click Here!</button>
  
    <script>
        function func() {
            // Selecting  div and
            // The text inside the div will be removed.
            var div = d3.select("span")
                .remove();
        }
        btn = document.querySelector("button");
        btn.addEventListener("click", func);
    </script>
</body>
  
</html>


Output:

Before clicking the click Here button:

After clicking the click Here button:

RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS