Ad

How I Get Height Of A Svg Element Using D3js

- 1 answer

i am using d3.js to find the height of an SVG element with the following code

<script>
    var body = d3.select("body");
    console.log(body);
    var svg = body.select("svg");
    console.log(svg);
    console.log(svg.style("height"));
</script>

<svg class="svg" height="3300" width="2550">
   <image x="0" y="0" height="3300" width="2550" xlink:target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="1.jpg"></image>
   <rect class="word" id="15" x="118" y="259" width="182" height="28"
      text="Substitute"></rect>
</svg>

but it returned an error

Uncaught TypeError: Cannot call method 'getPropertyValue' of null

Ad

Answer

You need to load js after the DOM is redered. You can either put the <script></script> in the bottom of your <body></body> or have it inside the following javascript method.

    document.addEventListener("DOMContentLoaded", function(event) { 
          //your code here
     });
Ad
source: stackoverflow.com
Ad