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

232
Views
change background-colour of clicked button in react:js

i have some buttons when ever i click on a button i want it heighlighted by changing the background colur of the button until any other button is cliked

function clickedtime(e) {
   var time = e.target.value;
   settime(time);
  
 }

<TabPanel className="tabcss" style={styles.tabpannel}>
                 <button
                   onClick={clickedtime}
                   value="9:00am"
                   style={styles.button}
                 >
                   9:00am
                 </button>
                 <button
                   onClick={clickedtime}
                   value="9:30am"
                   style={styles.button}
                 >
                   9:30am
                 </button>

                ....


               </TabPanel>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use event handler and useState for setting color onClick like this

const[btn,setBtn]=useState([0,0]);//mean two button

const[sel,setSel]=useState(0);//mean 0 button is selected by default
function clickedtime(e,index) {
var time = e.target.value;
 settime(time);
 setSel(index)//selected btn is 0in this case
}

now for your tab panel change style to style={{backgroundColor:col}} aslo change tabpanel styling like i mention

<TabPanel className="tabcss" style={styles.tabpannel}>
           btn.map((item,index)=>{
           sel==index?

           <button
               onClick={(index)=>{clickedtime(index)}}
               value="9:00am"
               style={{backgroundColor:"red"}}
             >
               9:00am
             </button>
           :
             <button
               onClick={clickedtime}
               value="9:00am"
               style={{backgroundColor:"white"}}
             >
               9:00am
             </button>
             


             }  
            ....


           </TabPanel>
about 4 years ago · Juan Pablo Isaza Report

0

You can add all buttons to a class, add event listener to them and then have another class called "clicked" attached to them when the click event is fired.

var buttons = document.getElementsByClassName("time-button");
var click_listener = function(event) {
  //console.log("clicked " + event.target);
  for (var i = 0; i < buttons.length; i++) {
    buttons[i].classList.remove("clicked");
  }
  event.target.classList.add("clicked");
};
for (var i = 0; i < buttons.length; i++) {
  buttons[i].addEventListener("click", click_listener, false);
}
.clicked {
  background-color: orange;
}
<button class="time-button">9:30 am</button>
<button class="time-button">9:00 am</button>
<button class="time-button">8:30 am</button>
<button class="time-button">8:00 am</button>

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!