Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will jQuery ever return a CSS measurement in something other than px?

I'm rewritting my Textarea Line Count plugin (<-- shameless plug), and have this question:

When I call $("#someElement").css("letter-spacing"), will I ever get a value in ems, or anything other than px? Based on this example: http://jsfiddle.net/xhhr2/, in Google Chrome at least, it appears that either jQuery or the browser is converting the measurement to px for me. Can I always expect this behavior?

like image 900
Chris Laplante Avatar asked Apr 21 '11 16:04

Chris Laplante


2 Answers

According to Crescent Fresh's answer that links to a hack by Dean Edwards, jQuery goes through great lengths to return the actual, computed pixel value across all browsers and not what was defined originally in the style sheet, so it seems that yes, you can rely on it.

like image 64
Pekka Avatar answered Nov 10 '22 10:11

Pekka


As far as I know, the browser has to convert every size you give it (whether it's in em, %. etc) into pixels. That's how the DOM stores it, and thus jQuery will return that value to you.

EDIT According to this answer here, (referenced by Pekka's answer) only IE supports the currentStyle, which gives the size (including the unit) that was set by CSS. Apparently, no other browser supports this, instead they all use computed style which converts everything into pixels. So I was about half-right. :P

like image 5
Dominic Barnes Avatar answered Nov 10 '22 11:11

Dominic Barnes