Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  99] [ 3]  / answers: 1 / hits: 19968  / 12 Years ago, thu, december 13, 2012, 12:00:00

Is it possible to append an SVG canvas to an element other than <body> using D3.js if the script is not inside that element?



For example, the code below does not work:



<html>
<body>
<script src=../../d3.v2.js></script>
<script>
var vis = d3.select(#container)
.append(svg:svg)
.attr(width,w)
.attr(height,h);
</script>

<div id=container></div>

</body>


All examples I have seen use d3.select(body).append.... but clearly we don't always want to append the canvas immediately to the body.



I found that if the <script> is inside the container div then I can use d3.select(#container) but it seems strange to me that I would have to include my script inside of the specific container(s) where I want the canvas.


More From » html

 Answers
23

You're trying to select #container before it exists. You'll need to put your code in an onload or move the script below #container in the page.


[#81435] Wednesday, December 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cierra

Total Points: 504
Total Questions: 108
Total Answers: 109

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
;