0

I know it's a very basic question, but still:

When a page is first loaded, the variable myvar is set to a random value: 0, 1, 2, or 3.

On button click, if the variable is less than 3, it's incremented by 1. If the variable = 3, it's set to 0.

Would be grateful for help form an jQuery expert!

3 Answers 3

1

Try this,

Live Demo

<input type="button" id="buttonId" value="click me" />

$('#buttonId').click(function(){
     // myvar = 3;
     if(myvar < 3)
         myvar++;
     if(myvar ==3)
         myvar = 0;
})
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much, this is just what I was looking for. I have q question to you: how can I pass these values to a PHP variable $my_var?
You can save in hidden field and access it on postback, or use jquery ajax for asynchronous call.
I'm sorry, @Adil, could you show me how to do that, please? Basically, I need to pass values 0 to 3 to a MySQL query, on click.
I think this shows how you can do it, stackoverflow.com/questions/8741517/…
1
$('#button').click(function(){
    myvar = (myvar == 3) ? 0 :  ++myvar;
    }
});

Comments

1

Something like this?

$('#button').click(function(){
    if (myvar< 3){
        myvar++;
    }else{
        myvar = 1;
    }
});

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.