2

If the height of an element was set as a percentage, can jquery return it as percentage?
I need to get a boolean information from it.

Alex
  • 34,899
  • 5
  • 77
  • 90
max88_it
  • 17
  • 7
  • 1
    similar question - http://stackoverflow.com/questions/744319/get-css-rules-percentage-value-in-jquery – Alex Feb 08 '12 at 16:45
  • How was it set as a percentage? Javascript? HTML's style or width attribute? – Evan Carroll Feb 08 '12 at 18:14
  • I don't have to know how it was set. I just would take a html document and get every "height" or "width" and known if it was set as a percentage. I would do it with jquery or something else.. – max88_it Feb 09 '12 at 16:54

2 Answers2

0

No. Percentage is relative. How is jQuery supposed to know what object to compare it against to know what the percent height is? That is why it is in px.

Barry Chapman
  • 6,690
  • 3
  • 36
  • 64
  • To be a bit clearer, object A is 80% the height of object B. When you ask for the % height of obj A, jQuery cannot assume that you are talking about the height relative to obj B. – Barry Chapman Feb 08 '12 at 16:58
  • Thanks. Any alternative to know if the height was set as a percentage? i want to get the information from the CSS for any object in the page and do actions only if it was not. Thank you again. – max88_it Feb 08 '12 at 17:03
  • No way to know unfortunately, unless you save state information. See this: http://jsfiddle.net/phfKW/ – Barry Chapman Feb 08 '12 at 17:12
0

Hopefully this is helpful.

I don't know of a way to convert it directly. However, my first thought is to find the px value of the window and do simple arthmetic based on that value. If you just wanted to have it for a specific element this could work.

$(yourElement).each(function ()
{
    var $windowHeight = $(window).height();
    var $elementHeight = $(yourelement).height();
    var $elementP = $elementHeight/$windowHeight;
    var $elementPBase = $elementP*100;
    var $elementPFinal = Math.round($elementPBase).toFixed(2);


});

There are much better ways to do the math here, but this is just to give you a quick idea.

user1197728
  • 181
  • 1
  • 1
  • 5