SVG get text element width
var bbox = textElement.getBBox();
var width = bbox.width;
var height = bbox.height;
and then set the rect's attributes accordingly.
Link: getBBox()
in the SVG v1.1 standard.
document.getElementById('yourTextId').getComputedTextLength();
worked for me in
Regarding the length of text the link seems to indicate BBox and getComputedTextLength() may return slightly different values, but ones that are fairly close to each other.
http://bl.ocks.org/MSCAU/58bba77cdcae42fc2f44