1

I have an issue with jquery .css function. I am using this to get the actual height of elements whose height is set to auto. Code which i am using is :

$(this).css({ height: $(this).css("height"), width: $(this).css("width") });

The issue is, the .css function sometimes returns "0px" sometimes "auto" and sometimes actual height (which is the desired behavior). Can anybody please tell me what i am doing wrong here ?

3 Answers 3

2

$(this).css("height") gives you the a defined (or default) value, while $(this).height() gives you the actual, 'measured' height.

From http://api.jquery.com/height/:

Get the current computed height for the first element in the set of matched elements or set the height of every matched element.

And from http://api.jquery.com/css/:

Get the value of a style property for the first element in the set of matched elements or set one or more CSS properties for every matched element.

Sign up to request clarification or add additional context in comments.

Comments

1

$(this).css("height") will return the set value of the CSS height property.

To get the physical height of the element, call $(this).height().

Comments

0

Cannot see purpose of your snippet, but using jquery, use that code which at least is cross browser:

$(this).css({ height: $(this).height(), width: $(this).width() });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.