Note: I am unable to utilize jQuery, only vanilla JavaScript
I am not very proficient in pure JS. Additionally, this time around, I cannot rely on any external resources such as jQuery.
What I am looking for:
If the div1 class is active, I want to hide text2
If the div2 class is active, I want to hide text1
I managed to get it working somehow, but my JavaScript does not respond when the class changes dynamically due to another piece of JavaScript code.
Code that activates the active class:
function activateClass(element) {
var allElements = document.getElementsByClassName('item');
for (var i = 0; i < allElements.length; i++) {
allElements[i].classList.remove('active');
}
element.classList.add('active');
}
Code that should trigger the hiding/showing when the class changes:
if (document.querySelector(".text2").classList.contains("active")) {
document.getElementsByClassName('text1s')[0].style.visibility = 'hidden';
document.getElementsByClassName('text2s')[0].style.visibility = 'visible';
}
if (document.querySelector(".text1").classList.contains("active")) {
document.getElementsByClassName('text2s')[0].style.visibility = 'hidden';
document.getElementsByClassName('text1s')[0].style.visibility = 'visible';
}
Where did I make a mistake?