Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda