Max
Max

Reputation: 2475

Select/Get html node from an element using D3

Let's say I have an html object called element that I create using bellow code:

var xmlString = "<div class="parent"><span class="child"></span></div>"
parser = new DOMParser()
var element = parser.parseFromString(xmlString, "text/xml");

// or simply using jquery
var string = "<div class="parent"><span class="child"></span></div>"
var element = $(string);

What I want to do is to select span.child from element using D3, not from the document. Using d3.select('span.child') will try to look for the <span class="child"></span> in the html document.

I checked the documentation and it says:

A selection is an array of elements pulled from the current document.

But I want to select not from the document but from the above element that I just created. Is there any way?

Upvotes: 0

Views: 826

Answers (1)

Max
Max

Reputation: 2475

After a bit of debugging I found out that if element is a object, not string, then d3.select(element) will not look for the element in the document instead it will return the element itself.

for detailed info:

  d3.select = function(node) {
    var group = [ typeof node === "string" ? d3_select(node, d3_document) : node ];
    group.parentNode = d3_documentElement;
    return d3_selection([ group ]);
  };

Upvotes: 1

Related Questions