1
I created the function to add the active class in the ID that was clicked, but if I click another I want you to remove the previous class and add active in the one that was currently clicked.
With document.querySelectorAll
I can, because it generates a Nodelist, so I make a forEach
and it works, but with getElementById
I’m not getting through.
function activeClass(event) {
event.preventDefault();
const element = event.target;
const id = element.getAttribute('id');
const article = document.getElementById(id);
article.classList.add('ativo');
}
Dude, I don’t understand your question, you want to add an id to an element when it is clicked on it and then remove the previous class ?
– Leandro Nascimento
This function compares if there are two identical ID’s on the page, if there is it will add the active class, but if I click another location I need you to remove the previous class and add it to the current one. I was able to explain?
– Randys Machado
You know that it is not correct to have two elements with equal ID, two ID with the same name always from the https://answall.com/questions/127330/qual-%C3%A9-a-purpose-of-properties-id-e-name-of-a-tag-html/
– hugocsl
But in my case here it is necessary, because I am using one element to interact with another.
– Randys Machado