I have a rather odd issue that I can't seem to solve. I have written a code and I want to use it when a certain element has this specific class name, so I have done this:
function slide() {
var box = document.getElementsByClassName('box');
if(box.style.maxHeight !== "100px") {
box.style.maxHeight = "100px";
box.style.opacity = "1";
} else {
box.style.maxHeight = "0";
box.style.opacity = "0";
}
return false;
}
And the HTML:
<h1 onclick="slide();"> Text </h1>
<div class="box">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.
</p>
</div>
And the CSS:
.box {
max-height: 0;
}
I think I am missing something here, but I don't know what. The funny thing is: when I would use getElementById
, it works! But I want to use classnames instead and that is not working.
The error I am getting is:
Uncaught TypeError: Cannot read property 'maxHeight' of undefined