I am trying to make an easy animation using click eventlistener but i would like to create it getting classes instead of ids. I would like whenever someone click the button to open the box and when it is clicked again to close it. I have tried it with ids and its working but when i use only classes it doesnt. What is the problem?
Thanks. html code:
<div class = "box"></div>
<button class = "btn">Click</button>
css code:
body {
background:grey;
}
.box{
width:0px;
height:0px;
background:orange;
}
.box.active {
width:300px;
height:300px;
background:orange;
}
javascript code:
var btn = document.getElementsByClassName("btn");
btn.addEventListener("click", openFunction, false);
function openFunction() {
var y = document.getElementsByClassName("box");
if ( y.className === "active" ){
y.className = "";
}else{
y.className = "active";
}
}