Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close and just later add HTML element in D3.JS?

Now I have the next code:

d3.select("#commiterStatsContainer")
        .append("div").attr("class", "chip")
        .append("img").attr("src", committers[i].avatar)
        .text(committers[i].login);

but it adds my text inside of the <img>...</img> tag. How can I close <img> and just later to add my text?

like image 926
J. Doe Avatar asked Dec 20 '25 16:12

J. Doe


1 Answers

The append method returns a new selection containing the appended elements (See documentation). So following your code:

d3.select("#commiterStatsContainer") // initial selection
  .append("div").attr("class", "chip") // new selection with a div
  .append("img").attr("src", committers[i].avatar) // new selection of img in that div
  .text(committers[i].login); // text for the img tag in the div.

Instead, try:

var div = d3.select("#commiterStatsContainer")
  .append("div").attr("...");

div.append("img").attr("....");
div.append("p").html("....");

The variable div is a selection of the newly created div, you then can use div.append() to append new elements to that div.

like image 72
Andrew Reid Avatar answered Dec 22 '25 09:12

Andrew Reid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!