Document.querySelector (...). offsetWidth returns null in Firefox.

I am using the following code in a javascript file (coffeescript):

myVar = document.querySelector('SVG > a:nth-child(2) > text').offsetWidth

      

On google chrome, this returns the width of a text element that is nested inside an a (anchor) element that is nested within an svg element as expected. In Firefox this returns NULL. I thought that maybe my selection syntax is not up to Mozilla standards, so I played with that too, but even if I could select the DOM element correctly (as the console output), I was unable to get any offsetWidth property; it always returned as undefined.

Now, in case it matters, my SVG is inlined into my page dynamically with some Javascript. It is placed directly in the body of the div and is enclosed in <svg>

and tags </svg>

.

What gives? How do I determine the width of these elements?

Thank!

+3


source to share


1 answer


NickFitz's answer at is what you need.

It suggests using the standard SVG feature getBBox()

.



myVar = document.querySelector (...).getBBox ().width

+2


source







All Articles