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

179
Visualizações
change state of button with document.getElementById("id").innerHTML

I have a button that display a certain text at first, but when i click it i would like it to display something else.

Here is the html input:

<label class="button">
<input type="checkbox" id="control" onchange="changeState();">
<span class="button1" style="color: #4CAF50">Pull</span>
</label>

and here is my function:

function changeState() {
  if (active == false) {
    active = true;
    start_timer();
    var myStop = "Stop"
    document.getElementById("control").innerHTML = myStop.fontcolor("#f00");
  } else {
    active = false;
    var myPull = "Pull"
    document.getElementById("control").innerHTML = myPull.fontcolor("#4CAF50");
  }
}

When i used <button></button> instead of <input> it used to work but i need to use the input for something else. any idea what i would have to change to get it to work? I'm really new to HTML and JavaScript so I'm sort of clueless right now.

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

0

the input is just a checkbox. As mentioned, there's no innerHTML. It's the span you want to change.

let button = document.getElementById('button1')

function changeState() { 
  let checked = document.getElementById('control').checked
  if (checked) {  
     let myStop = "Stop"
     button.innerText = myStop;
     button.style.color='red'
  }else{
     let myPull = "Pull"
     button.innerText = myPull;
     button.style.color="#4CAF50";
  }
}
<label class="button">
   <input type="checkbox" id="control" onchange="changeState();">
   <span id="button1" style="color: #4CAF50">Pull</span>
</label>

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is a solution without the span:

function changeState(e) {
  if (e.target.checked) {
    e.target.nextElementSibling.style.color = "#f00"
    e.target.nextElementSibling.textContent = "Stop"
  } else {
    e.target.nextElementSibling.style.color = "#4CAF50"
    e.target.nextElementSibling.textContent = "Pull"
  }
}

document.querySelector("#control").onchange = changeState;
<input type="checkbox" id="control" name="control">

<label for="control" class="button" style="color: #4CAF50">
Pull
</label>

You may want to go further and use a class and classList.toggle() to handle the fontcolor.

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