javascript - Get SVG text element's width -


i have following svg text element:

<text data-model-id=​"k10673" x=​"584" y=​"266" fill-opacity=​"1" style=​"font:​ 12px arial,helvetica,sans-serif;​ " fill=​"#a5d16f">​bu yil sonu​</text>​ 

it's approximately in middle of screen. , can inspect follows:

enter image description here

i need width of text. on chrome debugger, can see text has width of 83.941

enter image description here

however, cannot width javascript page. method related width returns zero, although it's correctly displayed chrome debugger.

i element follows:

var x = document.queryselector("#k11489 > g:nth-child(27) > text:nth-child(1)") 

then, following javascript functions return 0 or undefined:

x.getcomputedtextlength() x.getbbox().width x.style.width 

also i've tried following jquery functions after getting jquery object, however, none of them returns width either:

x.width() //returns function x.innerwidth() //returns null x.outerwidth() //returns null 

note: in order make sure x points text element, check them through console. following snapshots of javascript , jquery objects:

enter image description here

enter image description here

how can object's width in javascript or jquery?

you can use getbbox() (which part of svg 1.1) width of svg element below.

$("your_jquery_selector")[0].getbbox().width. 

if it's valid selector use

$("#k11489 > g:nth-child(27) > text:nth-child(1)")[0].getbbox().width 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -