0

I'm in the process of upgrading a web application from jQuery 1.5.2 to 1.7.1. In jQuery 1.5.2, it was possible to get the percentage CSS value of an element using $('#elem').css('width'), if the element was inside another hidden element.

To be clear, I need the CSS value in PERCENTS and I don't know beforehand whether it will be in percents, or px, or em, or whatever.

For a simple test case, see here: http://jsfiddle.net/strikernl/y3P3A/

It seemed to work in 1.5.2, then no longer in 1.7.1. I added 1.6 in the test just to see if that still worked, and it didn't, so it broke somewhere between 1.5.2 and 1.6 I guess.

Am I doing something wrong, or is there another way of doing what I'm trying?

"SOLVED", see my answer

strikernl
  • 55
  • 5
  • 14
  • see http://stackoverflow.com/questions/8387419/retrieving-percentage-css-values-in-firefox – stecb Dec 15 '11 at 08:23
  • That getStyle() function seems to do the trick, so if all else fails I guess I can use that. But it doesn't really explain why it worked in jQuery in the past and not now. Thanks in any case! – strikernl Dec 15 '11 at 08:30
  • No problem ;) btw its weird, you should check jquery's source to see the differences. – stecb Dec 15 '11 at 08:33

3 Answers3

0

are you looking for :

$('#foo').width();

you can also get the :

$('#foo').outerWidth()

or

$('#foo').innerWidth()

(depending on your padding, border...)

by the way, in jsfiddle, you can include directly jQuery (and many other librairies...look at the left sidebar)

Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
moabi
  • 1,381
  • 1
  • 9
  • 9
  • No, neither width(),outerWidth() nor innerWidth() do what I need. In fact, those don't even do what I need in jQuery 1.5.2, let alone 1.7.1. Also, if you had looked more closely at my example, you'd have seen that I load THREE versions of jQuery, not just the one. I don't think jsfiddle provides that option. I added a new test case so you can see the output of width(),outerWidth() and innerWidth() as well here: http://jsfiddle.net/strikernl/eenRY/2/ – strikernl Dec 15 '11 at 10:08
0

can't u do it like this:

$('#elem').width();

I've updated your fiddle here: http://jsfiddle.net/y3P3A/1/

Gerben van Dijk
  • 868
  • 6
  • 15
  • Errr, how does that help? You added the percentages literally (between the quotes). I don't in fact know beforehand it's going to BE a percentage. It's kind of the point of my question, actually. – strikernl Dec 16 '11 at 07:28
  • Ah ok, I figured you knew it was gonna be a percentage anyway. Nevermind then. – Gerben van Dijk Dec 16 '11 at 08:59
0

For anyone interested, I found out it's a bug. See here http://bugs.jquery.com/ticket/10782. It took quite a while to get noticed though apparently (1.6 through 1.6.4, then 1.7 and 1.7.1).

strikernl
  • 55
  • 5
  • 14