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

143
Visualizações
Checkbox automatically being selecting when button clicked, how can I stop this?

I am making a calculator and want a specific value to be assigned depending on if the checkbox is checked or not, the code I have might work however everytime I click the "Calculate" button it automatically checks the checkbox even if it wasn't checked in the first place. How would I fix this?

function run() {
  var checkbox = document.getElementById('side1');

  if (checkbox.checked = true) {
    var output = 5;
  } else {
    var output = 3;
  }

  document.getElementById("totalvalue").innerHTML = output;

}
<!DOCTYPE html>
<html>

<head>
  <meta charset=utf-8 />
  <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <title>Document</title>
</head>

<body>
  <form>
    <label for="side1">Side 1</label>
    <input type="checkbox" id="side1" name="side1"></input>

    <input type="button" value="Calculate" onclick="run()"></input>

    <span>Total: $</span>
    <span id="totalvalue"></span>
    <script type="text/javascript" src="tek.js"></script>
  </form>
</body>

</html>

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

0

You have a typo in tek.js

Since you are assigning true to checkbox.checked , it sets the checkbox (similar to checking the checkbox) and 5 is evaluated based on your logic.

You may want to set checkbox.checked == true to get desired output.

enter image description here

enter code here

about 4 years ago · Juan Pablo Isaza Relatório

0

I updated your code now. It's working as expected. Due to your if condition, your checkbox got selected.

i replaced if (checkbox.checked = true) with if (checkbox.checked == true). It is now working.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
 
 function run() {
  var checkbox = document.getElementById('side1');

  if (checkbox.checked == true) {
    var output = 5;
  } else {
    var output = 3;
  }

  document.getElementById("totalvalue").innerHTML = output;

}
</script>
</head>
<body>

<!DOCTYPE html>
<html>

<head>
  <meta charset=utf-8 />
  <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <title>Document</title>
</head>

<body>
  <form>
    <label for="side1">Side 1</label>
    <input type="checkbox" id="side1" name="side1"></input>

    <input type="button" value="Calculate" onclick="run()"></input>

    <span>Total: $</span>
    <span id="totalvalue"></span>
    <script type="text/javascript" src="tek.js"></script>
  </form>
</body>

</html>

</body>
</html>

   
 

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