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

263
Vistas
How do I manipulate a boolean in a function?

I'm trying to change inputX[0] from false to true, then get an alert if it worked. Unfortunately I don't get the message that inputX[0] was set to true. Do you have any ideas?

<body>
  <div>
    <button id="S1" onclick="btnManager(this);"></button>
  </div>

  <script>
    var inputX = new Array();
    definedInputs();
    btnManager();
    question();

    function definedInputs() {
      inputX[0] = false;
    }

    function btnManager(pressedBtn) {
      if (pressedBtn.id == (id = "S1")) {
        inputX[0] = true;
      }
    }

    function question() {
      if (inputX[0] == true) {
        alert("inputX is set to true");
      }
    }
  </script>
</body>

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

try with this:

<body>
  <div>
    <button id="S1" onclick="btnManager(this);"></button>
  </div>

  <script>
    var inputX = new Array();
    inputX[0] = false;

    function btnManager(pressedBtn) {
        inputX[0] = !inputX[0];
        alert(inputX[0]);
    }
  </script>
</body>

With this, every you push the button the value will be set by the negation of the current value, hope it helps.

about 4 years ago · Santiago Gelvez Denunciar

0

It is important that you understand the Js lifecycle. First javascript objects and functions are built and then the code is executed, in this case it happens as follows:

  1. The array "inputX" is created
  2. Using the function "definedInputs()" defines "inputX[0] = false"
  3. "btnManager()" is executed but since it is not assigned a parameter, the value of "pressedBtn.id" is "undefined" so the state of "inputX[0]" does not change
  4. The status of "inputX[0]" is queried using "question()", but since "inputX[0]" is still false, no alert is fired.

All of this happens before you can press the button. Pressing the button executes "btnManager(this)" and since the id is equal to "S1" the state of "inputX[0]" changes to true.

But you can't see this change because the "question()" function has already been executed.

Try:

<!DOCTYPE html>
<html>
<body>
  <div>
    <button id="S1" onclick="btnManager(this);">Test</button>
  </div>

  <script>
    var inputX = new Array();
    definedInputs();

    function definedInputs() {
      inputX[0] = false;
    }

    function btnManager(pressedBtn) {
      if (pressedBtn.id == "S1") {
        inputX[0] = true;
      }
      question();
    }

    function question() {
      if (inputX[0] == true) {
        alert("inputX is set to true");
      }
    }
  </script>
</body>
</html>
about 4 years ago · Santiago Gelvez Denunciar

0

Add some console logs to see what is happening. If you do, you'll see that all of your functions are executing immediately. When you click the button and enter the btnManager() function what happens next? As you'll see, your code is executing the btnManager() function but then what about checking for your alert?

If you call question() after your if statement, then you'll run your check again.

You could do this with less lines, but for the sake of keeping your exact code and making it work, this is how you would achieve your goal:

<body>
  <div>
    <button id="S1" onclick="btnManager(this);"></button>
  </div>

  <script>
    var inputX = new Array();
    // You could really remove these and do it like Lucretius's Answer
    definedInputs(); 
    btnManager();
    question();

    function definedInputs() {
      inputX[0] = false;
    }

    function btnManager(pressedBtn) {
      if (pressedBtn.id == (id = "S1")) {
        inputX[0] = true;
      }

      question();
    }

    function question() {
      if (inputX[0] == true) {
        alert("inputX is set to true");
      }
    }
  </script>
</body>
about 4 years ago · Santiago Gelvez 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