0

Here's my fiddle :

http://jsfiddle.net/JWww3/

I'm not sure to understand why this code is not working.

What I want to add +1 to the "ajout" variable when I click on the "+1" button. And if ajout = 5 (after 5 clicks)...then alert("Hello");

Why am I doing wrong?

Here's my code :

HTML :

<input type="button" onclick="clickit();" id="click1" value="+1"/>​

JS :

var ajout = 0;
function clickit()
{
 ajout +=1;    
}

if (ajout==5)
{
alert("TEST");   
}

Thank you!

4
  • What happens? What's the code that doesn't look work like? Please try to post details here as well as having a fiddle :) Commented Jun 20, 2012 at 18:17
  • You are doing everything wrong, learn some JS basics. Commented Jun 20, 2012 at 18:18
  • 1
    Can you add the code in question here so the question gets perserved for future generations to look to for inspiration? (links go bad over time) Commented Jun 20, 2012 at 18:18
  • The code has been added. Why am I getting a -1? Can't a beginner ask questions here? Thank you Commented Jun 20, 2012 at 18:22

6 Answers 6

4

I think this is what you want:

var ajout = 0;
function clickit()
{
    ajout += 1;

    if (ajout==5)
    {
        alert("TEST");   
    }   
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 this is the correct answer, if condition was never executing on click event since it was outside of function which is triggered on click.
Thank you, it's working. I studied the way you made it work and I understand it now. Thanks again
1

I suppose it should be

ajout += 1;

And what you have is

=+

1 Comment

No that's not the issue you can use either
1

Try this :

var ajout = 0;
jQuery('#click1').click(function(){
    ajout += 1;    
    console.log(ajout);
    if (ajout==5)
    {
    alert("TEST");   
    }
});

Here is the Demo

Comments

1

Select "no wrap (body)" option from jsfiddle. Otherwise clickit function is defined only after DOM has loaded, which means it could never get executed.

Also make sure to put that alert inside clickit function to make sure it gets executed when you call that function.

1 Comment

The alert call is not placed inside clickit, but is instead executed when the script is loaded. Move it inside clickit and it will be executed!
0

Try This:

<input type="button" onclick="clickit();" id="click1" value="+1"/>
<script>
var ajout = 0;
function clickit()
{
 ajout += 1;    
    if (ajout==5)
    {
        alert("TEST");   
    }
}

</script>

Comments

0

In addition to what the other 2 have said, your evaluation code should be within the function, otherwise it is NOT going to be called every time the button is clicked, which I think is what you are after.

var ajout = 0;
function clickit()
{
 ajout++;  
 if (ajout==5)
   alert("TEST");   
}

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.