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

188
Visualizações
How to get input from toggle switch JavaScript

I have this toggle switch in my JavaScript code:

var questionRequired = document.createElement("div");
  questionRequired.className = "mt-1 mb-1";
  questionRequired.insertAdjacentHTML(
    "beforeend",
    `<label class="custom-control ios-switch">
       <input type="checkbox" class="ios-switch-control-input" />
       <span class="ios-switch-control-indicator"></span>
       <span class="ios-switch-control-description">Required</span>
     </label>`
  );

It is showing nicely and is being turned on and off as we can see here.

Toggle switch

My problem is that it's not doing nothing. I want to to set a variable as true if it's turned on and to false when it's turned off.

Let's say I have var isRequired and I wan to set it through this toggle switch. I want to do something like:

if (questionRequired.isTurnedOn)
    isRequired = true;
else isRequired = false;

Does anyone kow how I can do this? Thank you.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Here is a simplified example.

Create an eventListener on your checkbox, as well as bool variable.

isChecked = isChecked ? false : true; basically means that if the value is false, set it to true, and the otherway around.

var chxBox = document.querySelector(".ios-switch-control-input");
var isChecked = false; //false because the checkbox is unchecked on page load
chxBox.addEventListener("change", function(){
isChecked = isChecked ? false : true;
console.log(isChecked)
})
 <input type="checkbox" class="ios-switch-control-input" />

about 4 years ago · Santiago Trujillo Relatório

0

try this

var checked = false
var questionRequired = document.createElement("div");
questionRequired.className = "mt-1 mb-1";
questionRequired.insertAdjacentHTML(
"beforeend",
`<label class="custom-control ios-switch">
   <input onChange="isChecked()" type="checkbox" class="ios-switch-control-input" />
   <span class="ios-switch-control-indicator">. 
   </span>
   <span class="ios-switch-control-description">Required</span>
 </label>`)

function isChecked(){
 checked = checked?false:true
 return checked
}
console.log(checked)
about 4 years ago · Santiago Trujillo 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