With code below I want to insert some <content>
once the user clicks on the <button>
.
Before I went with this code which was working totally fine. Then I changed the id="content"
to class="content"
and the document.getElementById
of the JavaScript code to getElementsByClassName
and now the function is not working anymore.
What do I have to change my code so it also works with classes?
You can also find my code here.
window.myFunction = function () {
var x = document.getElementsByClassName("content");
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
.content {
width: 80%;
padding: 10%
text-align: center;
background-color: lightblue;
margin-top:20px;
}
<button onclick="myFunction()">Button</button>
<div class="content">
Content of element
</div>