17

I have a div, and the maximum width for this div is user defined. I know I can get it done using element.style.height but this doesn't work in IE.

Any ideas on how to implement the max-height equivalent of Firefox by using javascript?

3 Answers 3

42

Usually style attribute names are translated into javascript property names by removing the hyphens and camelcase the name instead.

So background-color becomes backgroundColor, text-align becomes textAlign and max-height becomes maxHeight.

You can set an element el's maximum height to mHeight by:

el.style.maxHeight=mHeight;

Remember to use a valid value for mHeight.

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

2 Comments

Just for clarity, a "valid value for mHeight" would be a string which includes units, exactly as in CSS. E.g., mHeight = "100px", not mHeight = 100.
Just for more clarity, it may also be "0", a percentage string ("50%") or "none", all of these are valid values for max-height (the corresponding CSS property), see w3.org/TR/CSS21/visudet.html#min-max-heights
6
document.getElementById ( "yourelementid" ).style.maxHeight = "100px";

See maxHeight Property

maxHeight was introduced in Windows Internet Explorer 7

Comments

2

To make the animation time match what is expected, I would use el.scrollHeight to find the required height of the element.

2 Comments

Looks like this might be an answer to some other question...? :-) Or am I missing something? :-)
Yes, I meant to reply to the top answer. Sorry - I've not used StackOverflow much.

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.