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

168
Visualizações
How to pass value from html input to javascript correctly?

How can I pass value from html input to javascript I have value_1 = 0 but when I change it in input form I want code inside if to happen.

<body>
<input type="number" name="a" id="commands"><br>
<canvas id="myChart1"></canvas>

<script>
const value_1 = 0;
const value_1 = document.getElementById("commands").value;

if(value_1 != 0)
{
 //code
}
</script>    
</body>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

add a listener for the 'keyup' event, as follows:

document.getElementById("commands").addEventListener('keyup', (e) => {
   let value = document.getElementById("commands").value;
   if (value !== 0) {
      // code
   }
});

Or like this:

document.getElementById("commands").onkeyup = function(e) {
   let value = document.getElementById("commands").value;
   if (value !== 0) {
      // code
   }
}

or with jQuery:

$('#commands').on('keyup', (e) => {
   let value = $('#commands').val();
   if (value !== 0) {
      // code
   }
});
about 4 years ago · Juan Pablo Isaza Relatório

0

You can pass value from html to JavaScript correctly using following code:

HTML:

<form onsubmit="return getValue('commands')">
  <input type="number" name="a" id="commands">
  <canvas id="myChart1"></canvas>
</form>

JavaScript:

<script>
    function getValue(id) {
        let value_1 = 0;
        value_1 = document.getElementById(id).value;
        alert(value_1);
        
        if(value_1 != 0)
        {
         //code
        }
        return false;
    }
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use addEventListener with change event type or use the onchange property.

Example:

    <script>
    const value_1 = document.getElementById("commands");

    value_1.onchange = function(){
        if(value_1.value != 0)
        {
            console.log("Not 0");//code
        }
    }
    </script> 
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