Open In App

D3.js selection.each() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The selection.each() function in D3.js is used to call the particular function for each selected HTML elements. In function datum(d) and index(i) are given as the parameters. By using this method one can access parent and child data simultaneously.

Syntax:

selection.each(callback);

Parameters: This function accepts a single parameter as mentioned above and described below.

  • callback: This is the function that is invoked by each selected element.

Return Values: This function does not return anything.

Below given are a few examples of the function given above.

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.each() Function</title>
    </head>
    <style>
        .div {
            width: 200px;
            height: 200px;
            background-color: green;
            overflow: hidden;
        }
        div {
            background-color: red;
            width: 10px;
            height: 10px;
        }
    </style>
    <body>
        <ul>
            <li>Geeks for geeks</li>
            <li>Some text</li>
        </ul>
        <ul>
            <li>List tag</li>
            <li>each function</li>
        </ul>
        <button>Click me</button>
        <script src=
        </script>
        <script src=
        </script>
        <script>
            let btn = document.querySelector("button");
            let func = () => {
                let p = d3.selectAll("ul");
                p.each(function (p, j) {
                    d3.select(this)
                        .selectAll("li")
                        .text(function (d, i) {
                            return "child are edited.";
                        });
                });
            };
            btn.addEventListener("click", func);
        </script>
    </body>
</html>


Output:

Before clicking the button:

After clicking the button:

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.each() Function</title>
    </head>
    <style>
        div {
            background-color: green;
            margin-bottom: 5px;
            padding: 10px;
            width: fit-content;
        }
    </style>
    <body>
        <div>Some text</div>
        <div>Geeks</div>
        <div>Geeks for geeks</div>
        <div>Some text</div>
        <button>Click me</button>
        <script src=
        </script>
        <script src=
        </script>
        <script>
            let btn = document.querySelector("button");
            let func = () => {
                let p = d3.selectAll("div");
                console.log(p);
                p.each(function (p, j) {
                    console.log("p: " + p, "j: " + j);
                    d3.select(this).text(function (d, i) {
                        return "DIVs are edited.";
                    });
                });
            };
            btn.addEventListener("click", func);
        </script>
    </body>
</html>


Output:

Before clicking the button:

After clicking the button:
 



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