Answer the question
In order to leave comments, you need to log in
How can the add class function be improved?
I have a list of tabs, by clicking on the title of any of the tabs, the active class should be added to the title, and to the contents of this tab.
My code works, everything is OK, but I would like to know the best practice, how it would be worth doing.
<div class="tabs">
<div class="step_group">
<div class="step_list title">Title Tab 1</div>
<div class="step_options">Content Tab 1</div>
</div>
<div class="step_group">
<div class="step_list title">Title Tab 2</div>
<div class="step_options">Content Tab 2</div>
</div>
<div class="step_group">
<div class="step_list title">Title Tab 3</div>
<div class="step_options">Content Tab 3</div>
</div>
</div>
<script>
const stepGroup = document.querySelectorAll('.step_group');
stepGroup.forEach(element => {
const stepTitleElement = element.querySelector('.step_list .title');
const attrDescriptionSpoilerElement = element.querySelector('.step_options');
stepTitleElement.addEventListener('click', () => {
stepTitleElement.classList.toggle('active');
attrDescriptionSpoilerElement.classList.toggle('active');
})
})
</script>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question