Reputation: 1
How can i get one button to do multiple tasks depending on how many times it has been clicked? (Javascript)
For example button is clicked - alert "button clicked once" button gets clicked again - alert "button clicked twice" button is clicked again - alert" button has been clicked three times!"
Sorry if its a noob question, the reason is I AM A NOOB!!!
Upvotes: 0
Views: 1466
Reputation: 397
You could create a var that will increment when the button is clicked, and you can switch that var so you can do an action depending on the number of clicks for example:
var clicks = 0;
function someFunction(){
clicks++;
switch (clicks){
case 1:
//do something when its clicked only once
break;
}
}
And thats it.
Upvotes: 0
Reputation: 11255
Fiddle demo: http://jsfiddle.net/h9DbF/
in js:
var clickTimes = 0;
function doSomethingOnClick() {
clickTimes++;
switch(clickTimes) {
case 1:
alert('Button clicked once');
break;
case 2:
alert('Button clicked twice');
break;
default:
alert('Button clicked ' + clickTimes + ' times');
}
//Do something else
}
in html:
<button onclick="doSomethingOnClick()">Press me</button>
Upvotes: 2