-1

I have been trying for a good minute to figure out why my if statement never triggers. Any help would be much appreciated!

var clickcounter = 0;
var buttonclicker = document.getElementById("buttonclicker");

function unblur() {
    clickcounter = clickcounter + 1;
    console.log(clickcounter);
}

if (clickcounter === 1) {
    buttonclicker.textContent = "Unlock More";
    alert(clickcounter);
}

the variable clickcounter will be equal to or over 1. I at first tried using if (clickcounter => 1) and also tried if (clickcounter > 0) but none of this worked. What am I doing wrong? Here is a codepen of the whole thing: https://codepen.io/sage379/pen/JyppZv

4
  • 1
    You set clickcounter to 0 and then check if it's equal to 1. It's not 1, it's 0. Because it was just set to 0. Commented Aug 18, 2017 at 17:55
  • What calls unblur? Commented Aug 18, 2017 at 17:55
  • unblur is called by an onClick function, I left a lot of the code out, my mistake. codepen.io/sage379/pen/JyppZv is a codepen of the whole thing Commented Aug 18, 2017 at 18:10
  • @user3161849: Yes, but unblur isn't called until something else calls it. Whereas your if statement executes immediately. Did you mean to put the if statement in the unblur function? Commented Aug 18, 2017 at 18:19

4 Answers 4

1

Based on your code example, unblur() never gets called so clickcounter remains 0.

Sign up to request clarification or add additional context in comments.

4 Comments

and even if unblur does get called, the conditional has already tested the state of clickcounter so its too late.
@Octopus assuming that the code is written as it appears in the question. Sometimes users copy a few lines here, a few lines there, and put it all together in the question without realizing how much context they're throwing out. This was how I interpreted the code in the question.
I should've specified, unblur() is called.. and I know the variable is changed because I made it console.log the value of clickcounter.. here is a codepen of it: codepen.io/sage379/pen/JyppZv
Sure, you change clickcounter when you click the button but you never run that if statement again after that. Your code does the following: Set clickcounter to 0, check if clickcounter === 1. Then your function changes click counter and that's all your code does. You could call a function from within unblur that checks if clickcounter === 1 but currently you just increment the value and do nothing with it.
0

Please try with the below code snippet. Let me know if i did not understand your requirement.

var clickcounter = 0;
var buttonclicker = document.getElementById("buttonclicker");

       function unblur() {

            clickcounter = clickcounter + 1;
            console.log(clickcounter);


             if (parseInt(clickcounter) >= 1){
                  buttonclicker.textContent = "Unlock More";

             }
        }

Comments

0

Put your if statement inside of unblur:

var clickcounter = 0; 
var buttonclicker = document.getElementById("buttonclicker");
function unblur() {
 clickcounter = clickcounter + 1;
 console.log(clickcounter);
 if (clickcounter === 1) {
    buttonclicker.textContent = "Unlock More"; 
    alert(clickcounter);
 }
}

Comments

0

The problem is that once you increment clickcounter, you never check if it's changed. That check is performed once in your script, immediately after setting clickcounter = 0.

Here is an example where I've wrapped that check in a new function called checkClickCounter(). This function is called in unblur() each time it is called by your button click. I changed your if statement back to >= but since you're incrementing by one it will always return true after one click using === so this change doesn't matter in your current code.

var clickcounter = 0;
var buttonclicker = document.getElementById("buttonclicker");

function unblur() {
    clickcounter = clickcounter + 1;
    checkClickCounter(clickcounter);
}

function checkClickCounter(count){
    if (count >= 1) {
        buttonclicker.textContent = "Unlock More";
        //alert(clickcounter);
    }
}

Comments

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.