I just started learning Javascript today and decided to start with practical examples, for example a toggle function.
This is what I've been ended with so far:
document.getElementsByClassName("toggle").addEventListener("click", function() {
if(this.classList.contains("toggled")) {
this.classList.remove("toggled")
}
else {
this.classList.add("toggled")
}
})
However it returns the error: document.getElementsByClassName(...).addEventListener is not a function. I figure that I havn't declared the function
correctly, but can't figure why. Here's a Codepen.
It's probably less of a problem as what I see it.