Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
Why isn't the input box outputting the correct response

When the user clicks the buttons I want it to output the correct response.If the user puts 0 in the input box and then clicks the button I want it to say this is zero.If it 1 and higher I want it to say it positive number.If it -1 and lower it should say this is negative number.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>numberCheck</title>
    <style>
        body{
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>Number Check</h1>
    <input type="number" id="num">
    <button id="submit">Submit</button>
    <div id="store"></div>
    
    <script>
        const num = document.getElementById('num');
        const data = num.value
        const btn = document.getElementById('submit');
        const store = document.getElementById('store');

        btn.addEventListener('click',()=>{
            checker();
        });

        function checker(){
            const ele = document.createElement('div');
            if (data === 0){
                ele.innerHTML =data+' this is a zero';
                store.appendChild(ele);
            }
            if (data <0){
                ele.innerHTML =data+' this is a negitive number';
                store.appendChild(ele);
            }
            if (data >0){
                ele.innerHTML =data+' this is a positive number';
                store.appendChild(ele);
            }
        }
    </script>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

So first do not repeat yourself, if you want to create a new div and append it i'll recommend u to do it once in your function.

const ele = document.createElement('div');
store.appendChild(ele);

and because you are using input type number u have to get the value as integrer with valueAsNumber.

Then you have to update the value, yes because the value change while u are loading the value but never updating it. By the way for a changing value u can't use const but need to use a let or var.

btn.addEventListener('click', function() {
  let val = num.valueAsNumber;
  checker(val);
});

Here is a working version of your code : https://jsfiddle.net/4a17fsbn/

about 4 years ago · Juan Pablo Isaza Denunciar

0

> const ele = document.createElement('div');
>             if (num === 0){
>                 ele.innerHTML =num+' this is a zero';
>                 store.appendChild(ele);
>             }

You try to refer to the HTML element and compare it to zero. It's not. What you have to do is to check if the value of the input matches to your condition.

if (num.value == 0)

By the way, don't use triple equals here. The input value is always a string, but you compare it against a number, so "===" will always return a false here. Either explicitly convert input's value to a type of number or use double equals.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You write far too much code and your code is inefficient.

The main issue is, that the value of an input type="number"is not necessarily an integer. As soon as you write into the input with a keyboard then you get a string. To solve this you simply need to add a + before the string which converts it to an integer: +document.querySelector('#num').value.

As said above, your code is inefficient. One efficiency boost you can gain with textContent instead of using innerHTML which requires a re-parsing of the DOM. Then only write the actual necessary code and combine the eventListener with the function directly.

document.querySelector('#submit').addEventListener('click', function() {
  let nr = +document.querySelector('#num').value,
      el = document.querySelector('#store');
  if (nr === 0) {
    el.textContent = `${nr} this is a zero`;
  }
  if (nr < 0) {
    el.textContent = `${nr} this is a negative number`;
  }
  if (nr > 0) {
    el.textContent = `${nr} this is a positive number`;
  }
})
<h1>Number Check</h1>
<input type="number" id="num">
<button id="submit">Submit</button>
<div id="store"></div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda