0

I'm going nowhere with apparently a simple task of trying to set a boolean flag in Js / JQuery. I'm expecting that after btn1 click the flags should change (globally). But I likely misunderstand some inner works of JS and after clicking btn2 the status of flags stays exactly as initially declared - how can I go about this?

JS:

$(document).ready(function(){
  var $btn1 = $("#btn1");
  var $btn2 = $("#btn2");
  var active1 = true;
  var active2 = false;

  $btn1.click(function(){
    $active1 = false;
    $active2 = true;
    alert("after btn1 click: --> " + " active1: " + active1 + ", active2: " + active2);
  });

  $btn2.click(function(){
   // If clicked first on btn1 I'm expecting active1=false; active2=true; ??
        alert("after btn2 click: " + " active1: " + active1 + ", active2: " + active2);
  });
});

   <button id="btn1">Button1</button>
   <button id="btn2">Button2</button>

JsFiddle Here

2
  • 1
    change active1 = false; instead of $active1 = false; Commented May 25, 2016 at 12:57
  • This isn't PHP - active1 is one variable $active1 is another variable. Commented May 25, 2016 at 12:58

1 Answer 1

1

Remove $ from in declaration. Note active1 not $active1 same for active2.

$btn1.click(function(){
    active1 = false;
    active2 = true;
    alert("after btn1 click: --> " + " active1: " + active1 + ", active2: " + active2);
  });
Sign up to request clarification or add additional context in comments.

1 Comment

sure, I tried to wrap my head around a more complex case and I made a stupid mistake here. Good that it works though as expected, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.