Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
I want to add an active class to second span on a div with two spans with the same class with javascript

I want to add an active class to the second span and remove it from the first span when clicking on the " Click me" with javascript

  <span class="click">Click me<span/>.
    
    
    
    <div class="subtab">
      
      <span class="first active" data-tabname="reviews" tabindex="0">Reviews</span>
      
      <span class="first " data-tabname="questions" tabindex="0">Questions </span>
      
</div>
  
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This will work

HTML:

<div>
  <span class="span-class active">span 1</span>
  <span class="span-class">span 2</span>
</div>

<button id="btn">
alter
</button>

Script:

let spanElements = document.getElementsByClassName("span-class");
let btn = document.getElementById("btn");

btn.addEventListener('click', () => {
  if(spanElements[0].classList.contains('active')) {
    spanElements[1].classList.add("active");
    spanElements[0].classList.remove("active");
  } else {
    spanElements[0].classList.add("active");
    spanElements[1].classList.remove("active");
  }
});

CSS

.active {
  color: red;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!