I want to return, say, the width or height of a certain element without having set them beforehand with CSS or HTML. Is that even possible? Do you always have to set those before you can use them?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use the getComputedStyle() method on the element. There are some little differences across browsers on how this method works, but for the height and width properties it works the same.
So, an example would be:
Here’s a fiddle: http://jsfiddle.net/joplomacedo/7Qr83/