a beginner coder here (and also my first post). I was trying to make function that will change the value of a previously defined variable when the checkbox is checked or unchecked. I tried to write this code to see if the checked property of my checkbox is working.
function calc5() {
var check5Object = document.getElementById("check5");
stat5 = check5Object.checked;
console.log(stat5);
}
When i checked the console, it works out like expected. When the checkbox is checked it logs true and when the checkbox is unchecked it logs false on the console like this. I thought it was working smoothly... until i added the conditionals. The code looks like this:
function calc5() {
var check5Object = document.getElementById("check5");
stat5 = check5Object.checked;
if ((stat5 = true)) {
bobux5 = 10000;
} else {
bobux5 = 0;
}
console.log(stat5);
}
And the result on the console looks like this
Everytime I click the checkbox, it just logs as "true" all the time. Can someone help me figure it out what went wrong in my code? It will be very helpful! (btw im making a robux "scam" website that will rickroll you at the end, its harmless)
CodePudding user response:
You need to use == and rather use =
From:
if (stat5 = true) {
bobux5 = 10000;
}
to:
if (stat5 == true) {
bobux5 = 10000;
}
CodePudding user response:
Use this code:
function calc5() {
var check5Object = document.getElementById("check5");
var bobux5 = 0;
stat5 = check5Object.checked;
if (stat5 == true) {
bobux5 = 10000;
}
console.log(stat5);
}
in fact you should use == in conditions. => (if == X TURE) and (if = X FALSE)