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

119
Visualizações
Make button text appear in text field, and also remove background color when other button is c licked?

So, what I'm trying to do here for a JavaScript course is to make 5 buttons. When I click a button, I want the button text appear in the text field. For instance, when I click Red, I want the text "red" to appear in the field, and also for the background in the field to be red.

I've gotten the background part, but I'm unsure how to make the text appear..?

Also, I'd like that if I press Red and then blue, the red background is "removed". Right now, if I click red and then blue, then click blue again - the background goes back to red..

Any ideas?

function toggleStyle1() {
  var onclick = document.getElementById('field').classList.toggle('first-style');

}

function toggleStyle2() {
  var onclick = document.getElementById('field').classList.toggle('second-style');

}

function toggleStyle3() {
  var onclick = document.getElementById('field').classList.toggle('third-style');

}

function toggleStyle4() {
  var onclick = document.getElementById('field').classList.toggle('fourth-style');

}

function toggleStyle5() {
  var onclick = document.getElementById('field').classList.toggle('fifth-style');

}
.first-style {
  background-color: rgb(238, 0, 0);
  font-weight: bolder;
}

.second-style {
  background-color: rgb(0, 255, 76);
  font-weight: bolder;
}

.third-style {
  background-color: rgb(17, 0, 255);
  font-weight: bolder;
}

.fourth-style {
  background-color: rgb(176, 0, 199);
  font-weight: bolder;
}

.fifth-style {
  background-color: rgb(255, 13, 162);
  font-weight: bolder;
}
<button id="red" onclick="toggleStyle1('red')">red</button>
<button id="green" onclick="toggleStyle2('green')">green</button>
<button id="blue" onclick="toggleStyle3('blue')">blue</button>
<button id="purple" onclick="toggleStyle4('purple')">purple</button>
<button id="pink" onclick="toggleStyle5('pink')">pink</button>
<br />
<p>Du har valgt:</p><input type="text" id="field" />

about 4 years ago · Juan Pablo Isaza
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