KZ Power
KZ Power

Reputation: 93

How can I change the global value when clicked a button jQuery?

I face a challenge that I need to change the global value. See code below, whenever user click the #first, it will count the times and store on the server side. However, I want to achieve that if I click the second, the global value become 1. Therefore, when I clicked #first again, it start from 1 again. How can I achieve that, appreciate?

var count=1;//global value

$('#first').on('click',function(){	
     count++			
});

$('#second').on('click',function(){	
     //count--(minus)?? make the global value 
     //become 1 no matter how many time I have clicked #first		
});

Upvotes: 1

Views: 55

Answers (1)

Ritesh  Karwa
Ritesh Karwa

Reputation: 2254

Javascript

var count=1;//global value

    $('#first').on('click',function(){  
             count++ ;          
    });

    $('#second').on('click',function(){ 
             count=1; //make the global value become 1 no matter how many time I have clicked #first        
    });

Upvotes: 3

Related Questions