Reputation: 1607
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>
Upvotes: 0
Views: 2859
Reputation: 36127
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);
});
Upvotes: 1