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

288
Views
¿Cómo cambio el HTML interno de un elemento <form-check form-switch> en js?

Este es mi código html :

 <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>

Este es mi js :

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

No sé por qué el texto no está cambiando. ¿Podrías ayudarme?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Está seleccionando el elemento <input> , pero el que contiene el texto es el elemento <label> junto a la entrada.

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

Escuche el evento de change en la input en lugar de hacer clic. El evento de cambio se activará siempre que la entrada haya sido marcada o desmarcada y es más confiable para este caso.

Luego verifique la propiedad .checked en la entrada para ver si la casilla de verificación se ha marcado o no. En lugar de modificar la propiedad .innerHTML , use .textContent para establecer el texto modificado.

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