I have a div element with 2 classes that I would like to toggle a third class on depending on whether another element is active.
The other element's 'active' class is working, but I cannot work out how to toggle 'class3' on an element - I am getting the error
Uncaught TypeError: Cannot read property 'toggle' of undefined
but I am unsure exactly what about this function is incorrect. I'm still learning so this may be obvious!
HTML:
<div class="class1 class2"></div>
JS:
document.getElementById("button").addEventListener("click",
myFunction);
function myFunction(){
if (document.getElementById("otherElement").classList.contains("active")){
document.getElementsByClassName("class2").classList.toggle("class3");
}}
I also want to do this in pure JS, even if jQuery may be easier just to learn from the ground up