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

203
Views
Javascript: Change element class if button has a certain value

        <div class="main-div main-div2"> <!-- IF up to date, main-div3 -->
          <button id="update-btn" class="btn update-btn"> 
              Up to date
          </button>
        </div>

I want to make it so if update-btn has a value of "Up to date", change the div class from "main-div main-div2" to "main-div main-div3". Otherwise, if it's any other value, change it to "main-div main-div2"

What kind of loop would be good for this Javascript function too if I want it to be checking constantly?

Thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

there are many method to do it, here is the basic idea that you can get it done.

var btnText = document.getElementById("update-btn").innerText
var divClass = document.getElementById("outer-div")

if(btnText == "Up to date"){
    divClass.classList.remove("main-div2");
    divClass.classList.add("main-div3");
}else{
    divClass.classList.remove("main-div3");
    divClass.classList.add("main-div2");

}
.main-div2{
  background-color:red;
}

.main-div3{
  background-color:blue;
}
 <div id="outer-div" class="main-div main-div2">
   <button id="update-btn" class="btn update-btn"> 
     Up to date
   </button>
</div>

checkout the above snippet, its checking the text In if condition and sets up the class for parent div according to the text in the button, you can read more about adding and removing class from here

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!