Friday, June 12, 2026
HomeLanguagesJavascriptD3.js selection.exit() Function

D3.js selection.exit() Function

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(["neveropen", "for", "neveropen"])
                // 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:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS