Sunday, February 22, 2026
HomeLanguagesJavascriptD3.js selection.each() Function

D3.js selection.each() Function

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 neveropen</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 neveropen</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:
 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS