@SasaVatic
Posted
You could use forEach method to avoid duplicate code like this:
button.forEach((btn, index) => btn.addEventListener('click', function() { box[index].classList.toggle("text-visible"); box[index].classList.toggle("text-hide"); head[index].classList.toggle('bold-text'); }));
so foreach btn in button node list you can access btn element and its index and add click event to it and use index argument to access box and head node list elements with same index as btn which is clicked. I think it will also be better when you use querySelectorAll to use words in plural buttons, boxes, heads for naming your variables. Sorry for bad english, hope it helps.
Marked as helpful
@Andrii-Rohov
Posted
@ProtivProliv ohh right! it is super helpful)i totally forgot about forEach. I learn js fundamentals but when it comes to implement them i just can't figure it out. And variables names was from version #1 of my code) and it start to work only in ver. #5 or smth. But anyway, thanks a lot 👍