Skip to content
Related Articles

Related Articles

Improve Article

D3.js selection.each() Function

  • Last Updated : 18 Aug, 2020

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:

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.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:
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :