I wrote the following code and it works, but people are telling me not to use the onclick. How do I implement an alternative in a case like this? I'm replacing the "hidden" class with the "block" class, and vice versa. Would I be wrong to use this in production, or is there a better alternative as people have mentioned to me.
function myFunction() {
var element = document.getElementById("menu");
if (element.classList.contains("hidden")) {
element.classList.replace("hidden", "block");
} else if (element.classList.contains("block")) {
element.classList.replace("block", "hidden");
}
}
.hidden {
display: none;
}
.block {
display: block;
}
.icon {
fill: currentColor;
height: 24px;
width: 24px;
}
<button onclick="myFunction()">
<svg class="icon" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><title>Menu</title><path d="M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z"/></svg>
</button>
<div id="menu" class="hidden">
<p>I'm no longer hidden.</p>
</div>