Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!