I'm creating a schematic of tooltips, so when clicking on a certain span, this span shows a message
HTML
<div class="banner-tooltips" id="banner-tooltips">
<span id="primeira-tooltip">1°</span>
<span id="segunda-tooltip">2°</span>
<span id="terceira-tooltip">3°</span>
<span id="quarta-tooltip">4°</span>
</div>
JS
function tooltips(){
var caixaTooltips=document.getElementById("banner-tooltips");
var tooltips=caixaTooltips.querySelectorAll("span");
for(var i=0;i<tooltips.length;i++){
tooltips[i].addEventListener("click",function(){
var anterior=document.getElementsByClassName("banner-tooltip-active");
console.log(anterior);
if(anterior.length!=0){
console.log(anterior);
anterior.classList.remove("banner-tooltip-active");
}
console.log(anterior);
this.classList.add("banner-tooltip-active");
});
}
}
tooltips();
When you click on the first tooltip, it opens a good one, however, when you click it a second time, it says that you can not remove the undefined class. Why does this occur, and how can I resolve the problem?