0

I'm trying to access this property:

$(text)[0].offsetTop

It works fine in Chrome, but in Firefox I get 'undefined'. Is there any way to do this in all browsers?

See: http://jsfiddle.net/qgqr5m6n/3/

$("body").css("margin", "0px");
var svgNS = "http://www.w3.org/2000/svg";

var svgBox = document.createElementNS(svgNS, "svg");
$("body").append(svgBox);  
var text = document.createElementNS(svgNS, "text");
$(svgBox).append(text);

text.innerHTML = "Hello World";

$(text).attr({ "dominant-baseline": "hanging"});
console.log($(text)[0].offsetTop);

$(text).attr({ "x": 0 , "y":  0  });

In case you are wondering what I need this for: I want to align a text of variable font-size in the VERTICAL center of a rectangle. The font size, however, always includes a little gap above the actual top of the letter. What I want is the actual pixel height. This gap above the letter is exactly the value of "offsetTop" after I set "dominant-baseline" to "hanging".

Linus
  • 1,574
  • 2
  • 15
  • 23
  • Possible duplicate of [this](http://stackoverflow.com/questions/6777506/offsettop-vs-jquery-offset-top) –  Sep 20 '14 at 17:03

1 Answers1

1

Instead $(element)[0].offsetTop, use $(element).offset() that returns an object with Top and left.

afmeva
  • 839
  • 7
  • 13