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

171
Vistas
Disabling button until condition is met (javascript)

I'm not sure how to make a button disabled until a variable is equal to a number. In this case, I am trying to make the button disabled unless Cash is equal to 1.

<html>
<body>
    <button>Tester</button>
</body>
<script>
    var Cash = 0;

    if (Cash = 1) {
        btn.disabled = false;
    } else {
        btn.disabled = true;
    }
</script>
</html>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

var Cash = 0;
while(true) {
    if (Cash = 1) {
        btn.disabled = false;
    } else {
        btn.disabled = true;
    }
}

This will make it constantly check whether Cash is 1. In your current code, it will only check when the page is first loaded.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Ignoring proxies, no, what you are trying to do here is not really possible with how you are going about it.

What you should do instead is to create a function that changes the value of cash.

var Cash = 0;
function updateCash(newCash){
    Cash = newCash;
    if (Cash===1) {
        btn.disabled = false;
    } else {
        btn.disabled = true;
    }
}
updateCash(10);

You should know that

  • The equals sign = is for assignment. Your if statement actually ends up setting cash to 1. Use the (strict) equality operator instead ===
  • You can shorten enabling btn to btn.disabled = cash!==1
  • Use let instead of var and cash should be lowercase.

Please do not use while(true) under any circumstances. Since javascript is single threaded, this means that anything else that is running, including changing cash will never happen because it is stuck in the loop. You will probably find that the fans on your computer will begin spinning very loudly.

about 4 years ago · Juan Pablo Isaza Denunciar

0

you need to get the button element first by using document.querySelector

let btn = document.querySelector('button') 

You can read about querySelector here.

now when you selected the element you need to make sure that you're using == in your if condition instead of =

== is for comparing value = is for assigning value

Difference between = and ==

now you're code should look like this and it should disabled the button.

let cash = 0;

if (cash == 1) {
  btn.disabled = false;
} else {
  btn.disabled = true;
}

You can check this codepen

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