1

Say I have a .box class in CSS:

.box
{
   height: 50px;
}

Now, I use javascript to allow the user to expand the box and make it small again. Currently I have the value of the box height (ie 50 px) hardcoded in javascript.

I'm wondering if there's a way where my javascript code can access the height attribute of the box class as defined in CSS so that if I change the height in CSS to 100px for example, I wouldn't have to go and manually change the corresponding variable in javascript

btw I'm using rails3 and jquery

2 Answers 2

3

With jQuery it's pretty easy, although you have to use some trickery to get around the fact that jQuery's height() function spits back 50px instead of 50.

Just for getting the object's height, try this:

$(".box").height();

It doesn't succumb to the issue I stated above. The following example does, and fixes it. It can also be used for every other CSS attribute.

var boxHeight = $(".box").css("height").replace("px", "");

Docs here.

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

Comments

0
document.getElementById("mybox").style.height = myheight + "px";

2 Comments

What do you mean? You can call this more than once.
Isn't that exactly what you want?

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.