Han Jo. Jang
Han Jo. Jang

Reputation: 21

d3.select does not grab right tag

I am working on javascript project.

What I have done so far with d3.js is drawing a world map. I reference the following page.(http://www.d3noob.org/2013/03/a-simple-d3js-map-explained.html)

All countries are drawn under svg tag.

What I would like to do is convert the image to png and download to local computer.

Researching in the Internet, it id doable.

I need to convert it to base64 datarurl and then convert it to png.

The first thing I have to do is select svg tag.

The following is my code

function downloadImg(){     
        var html = d3.select("svg")
                .attr("version", 1.1)
                .attr("xmlns", "http://www.w3.org/2000/svg")
                .node().parentNode.innerHTML;

        window.alert(html);
        ...
        ...
}

For testing purpose, I print out "html". What it give to me is the entire body. I just need to have svg tag elements.

Can you tell me what I have done wrong?

Thank you.

Upvotes: 0

Views: 117

Answers (1)

meetamit
meetamit

Reputation: 25157

Element.outerHTML is your friend here:

var html = d3.select("svg")
                .attr("version", 1.1)
                .attr("xmlns", "http://www.w3.org/2000/svg")
                .node().outerHTML

Upvotes: 1

Related Questions