I have a section of code that is supposed to toggle an object's height to grow when clicked, and if clicked again it shrinks to it's original height. I tried two methods to get this to work. The second method does work, while the first doesn't work repeatedly.
As I understand it, when I set a height in Javascript it effectively adds it inline on the "style" attribute in HTML, and as such overrules the CSS stylesheet height setting, and the object grows.
However, the first method only works once, and I don't know why. In Method 1, the object grows when clicked, shrinks on the second click, and then doesn't work again.
The second method grows on the first click using the same methodology as Method 1, however, to shrink it simply removes the styling that was added. And it works over and over again without issue.
Why doesn't Method 1 work over and over again, while Method 2 does?
Method 1
if(document.getElementById("block1").style.height < 500) {
document.getElementById("block1").style.height = "500px";
}
else {
document.getElementById("block1").style.height = "200px";
}
Method 2
if(document.getElementById("block1").style.height < 500) {
document.getElementById("block1").style.height = "500px";
}
else {
document.getElementById("block1").style.height = "";
}