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

162
Visualizações
Toggle Button text After Selecting Option HTML

So, I've been trying to toggle a button text after selection an certain option.

However no matter what I try the button text doesn't change.

I have this HTML code:

<select id="temp">
        <option value="selectOption" disabled selected>Select</option>
        <option value="farenheit">Farenheit</option>
        <option value="kelvin">Kelvin</option>
</select><br>

<button id="buttonTemp" onclick="converterTemp()">Convert</button><br>

And this is the JS code:

const slt = document.querySelector('select');

switch (slt.value) {
    case 'farenheit':
        document.getElementById('buttonTemp').innerHTML = 'Convert to Fº';
        break;

    case 'kelvin':
        document.getElementById('buttonTemp').innerHTML = 'Convert to kº';
        break;
}

It was supposed to toggle the text "Convert" to "Convert to Fº" / "Convert to Kº" on the user selection of Farenheit or Kelvin.

Could someone help me, please?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Put the switch code into an event listener, that listens for a change in the dropdown.

const slt = document.querySelector('select');

slt.addEventListener('change', () =>
{
  switch (slt.value) {
      case 'farenheit':
          document.getElementById('buttonTemp').innerHTML = 'Convert to Fº';
          break;

      case 'kelvin':
          document.getElementById('buttonTemp').innerHTML = 'Convert to kº';
          break;
  }
})
<select id="temp">
        <option value="selectOption" disabled selected>Select</option>
        <option value="farenheit">Farenheit</option>
        <option value="kelvin">Kelvin</option>
</select><br>

<button id="buttonTemp" onclick="converterTemp()">Convert</button><br>


  

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