Open In App

D3.js selection.exit() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The d3.selection.exit() function in D3.js is used to remove the elements or tags which correspond to the old data or in simple words This is used to update the DIV elements that were created before with the new data given.

Syntax:

selection.exit();

Parameters: This function does not accept any parameter.

Return Values: This function returns the exit selection.

Below examples illustrate the D3.js selection.exit() function in JavaScript:

Example1:

HTML




<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta
            name="viewport"
            path1tent="width=device-width, 
                       initial-scale=1.0"/>
        <title>D3.js selection.exit() Function</title>
    </head>
    <style>
        div {
            background-color: green;
            color: #ffffff;
            width: 100px;
            margin-bottom: 5px;
            padding: 18px;
            box-sizing: border-box;
            height: 60px;
        }
    </style>
    <body>
        <!-- Please note that no div tags are added here -->
        <script src=
        </script>
        <script src=
        </script>
        <script>
            
            // This will create DIVs having data as given
            var div = d3
                .select("body")
                .selectAll("div")
                .data(["geeks", "for", "geeks"])
                // Old dataset
                .enter()
                .append("div")
                .text(function (d) {
                    return d;
                });
  
            div = div.data(["DIVS UPDATED", "GEEKS", 
                            "FOR", "GEEKS"], function (d) {
                return d;
            }); //Updated new data set.
            div.enter()
                .append("div")
                .text(function (d) {
                    return d;
                });
            div.exit().remove();
        </script>
    </body>
</html>


Output:

Example 2:

HTML




<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta
            name="viewport"
            path1tent="width=device-width, 
                       initial-scale=1.0"/>
        <title>D3.js selection.exit() Function</title>
    </head>
    <style>
        div {
            background-color: green;
            color: #ffffff;
            width: 50px;
            margin-bottom: 5px;
            padding: 20px;
            height: 10px;
        }
    </style>
    <body>
        <!-- Please note that no div tags are added here -->
        <script src=
        </script>
        <script src=
        </script>
        <script>
            var h2 = d3
                .select("body")
                .selectAll("h2")
                .data(["I am from heading level 2"])
                .enter()
                .append("h2")
                .text((d) => {
                    return d;
                });
  
            // Updated heading
            h2 = h2.data(["Heading Updated"], function (d) {
                return d;
            });
            //Updated new data set.
            h2.enter()
                .append("h2")
                .text(function (d) {
                    return d;
                });
            h2.exit().remove();
  
            var span = d3
                .select("body")
                .selectAll("span")
                .data("I am from span")
                .enter()
                .append("span")
                .text((d) => {
                    return d;
                });
  
            // Updated span
            span = span.data(["SPAN UPDATED ", "GEEKS", 
                              "FOR", "GEEKS"], function (d) {
                return d;
            }); //Updated new data set.
            span.enter()
                .append("span")
                .text(function (d) {
                    return d;
                });
            span.exit().remove();
        </script>
    </body>
</html>


Output:

Before using exit() function:

After using exit() function:



Last Updated : 13 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads