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

299
Visualizações
How do I change the innerHTML of a <form-check form-switch> element in js?

This is my html code:

<div class="form-check form-switch">
    <input class="form-check-input" type="checkbox" id="mySwitch" name="emp">
    <label class="form-check-label" for="mySwitch">EmpName</label>
</div>

This is my js:

var Switch = document.getElementById("mySwitch");
Switch.addEventListener("click",function(){
    if(Switch.value=="yes")
        Switch.innerHTML="EmpNo";
    else
        Switch.innerHTML="EmpName";
})

I don't know why the text isn't changing. Can you please help me?

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

0

You're selecting the <input> element, but the one containing the text is the <label> element next to the input.

const input = document.getElementById("mySwitch");
const label = input.nextElementSibling.

Listen for the change event on the input instead of click. The change event will be triggered whenever the input has been checked or unchecked and is more reliable for this case.

Then check the .checked property on the input to see of the checkbox has been checked or not. Instead of modifying the .innerHTML property, use .textContent to set the changed text.

input.addEventListener('change' function() {
  if (input.checked) {
    label.textContent = 'EmpNo';
  } else {
    label.textContent = 'EmpName';
  }
});
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