akoprowski
akoprowski

Reputation: 3315

D3: How to insert an HTML element into the DOM?

d3.insert, d3.append and d3.html are three ways to insert new elements into the DOM using d3.

However, they all expect HTML text and create new nodes. In my use case I already have a DOM element ready (for the curious: obtained by $compile'ing AngularJS snippet) and am at a loss how to add that. Help appreciated.

Upvotes: 0

Views: 836

Answers (1)

Scott Cameron
Scott Cameron

Reputation: 5323

From the docs for selection.append and selection.insert, these functions' argument can be "specified either as a constant string or as a function that returns the DOM element to append".

So if you have a node already assigned to variable my_node you can just do this:

selection.append(function() { return my_node; });

Upvotes: 3

Related Questions