I've been looking around on some collapsables, most of them seem to use some extra code that doesn't seem to be needed or I can't understand haha. anyways, I made a super simple one and in my mind and logic it SHOULD work. but apparently it gets the error of
app.js:4 Uncaught TypeError: Cannot read property 'style' of null
at btn (app.js:4)
at HTMLButtonElement.onclick (index.html:9)
if someone would be so kind to properly explain to me WHY this isn't working so I might understand the logic better? here is snippet:
var content = document.querySelector(".content");
function btn(){
if (content.style.display === "none") {
content.style.display = "block";
} else {
content.style.display = "block";
}
}
.content {
display: none;
overflow: hidden;
color: red;
}
<button type="button" onClick="btn();">collapsible</button>
<div class="content">
<p> inner content bla bla bla</p>
</div>
Thanks in advance! /Thuse
getElementById("content");toquerySelector(".content");You use content class every where but are programming the javascript to look for the id.datailselement for that: developer.mozilla.org/en-US/docs/Web/HTML/Element/detailsnoneinstead ofblockagain in else condition. preferably move that querySeelector inside the function and not above the html codes as a global variable. @Thuse