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

261
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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