How do I get the width of an svg element using d3js?

if you have an SVG element w/ id=frame...

 frame = d3.select('#frame')
                     .attr('class', 'frame')

            fh = frame.style("height").replace("px", "");
            fw = frame.style("width").replace("px", "");

fh and fw can now be used in math expressions.

you can also drop back to jQuery

  fw = console.log($("#frame").width());
  fh = console.log($("#frame").height());

which are numbers and can be used in math expressions


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

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

Just place your script after svg element is loaded by the browser and all will be fine.