Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Button do new thing each time.

1. ## Button do new thing each time.

How could a make a html button that each time your press it it does a new things?

• No idea what you mean, but perhaps:-

Code:
```<input type = "button" value = "Do Things" onclick = "dothings()">

<script type = "text/javascript">
var count = 0;
function dothings() {
if (count == 0) {dothis()}
if (count ==1) {dothat()}
if (count == 2) {dosomethingelse()}
if (count > 2) {doyetanotherthing()}
count ++;
}
</script>```
I think it amazing that we have four players in the semi-final. - Wimbledon Tennis Commentator, BBC2

• I agree with 'Philip M's post in that I don't understand the question.

But giving alternate solutions bases on his assumptions, I came up with
Code:
```<input type = "button" value = "Do Things" onclick = "dothings()">

<script type = "text/javascript">
var count = 0;
function dothings() {
switch (count) {
case 0 : dothis(); break;
case 1 : dothat(); break;
case 2 : dosomethingelse(); break;
default : doyetanotherthing(); break;
}
count++; count = (count % 4);  // round-robin back to first option
// or possibly
//  count = (count++ % 4);  // untested
}```

• Here is a different approach ...

Code:
```<script>
(function(a){
doNewThings = function(){
var b = a.shift()
else b();
}
</script>
<input type="button" onclick="doNewThings()">```

• Or perhaps

Code:
```function dothings() {
var num = 2;
var randy = Math.floor(Math.random() * (num+1)); // generates 0-2
if (randy == 0) {dothis()}
if (randy ==1) {dothat()}
if (randy == 2) {dosomethingelse()}
}```
Who can tell what is really wanted?

jmrker - count = (count++ % 4); // untested - does not work (yields 0 always and count is never incremented)

• Originally Posted by Philip M
...

jmrker - count = (count++ % 4); // untested - does not work (yields 0 always and count is never incremented)

Thanks ... it was just a shot off the top ... good to remember to NOT be so fancy at times.

Code:
```<script>
function two() { alert("2 2 2 2 2 2"); }
function three() { alert("Third time's a charm!"; }
function four() { location.href = "http://www.google.com"; }

var things = [ one, two, three, four ];

function clickit()
{
(things.pop())();
}
</script>
...
<input type="button" value="do something" onclick="clickit()" />```
Of course, as written you'll only do each thing once with that code, but he never asked to be able to repeat.

• Originally Posted by Old Pedant

Code:
```<script>
function two() { alert("2 2 2 2 2 2"); }
function three() { alert("Third time's a charm!"; }
function four() { location.href = "http://www.google.com"; }

var things = [ one, two, three, four ];

function clickit()
{
(things.pop())();
}
</script>
...
<input type="button" value="do something" onclick="clickit()" />```
Of course, as written you'll only do each thing once with that code, but he never asked to be able to repeat.
This one repeats ....

Code:
```<script>
(function f(arg){
var a = arg.slice(0);
doNewThings = function(){
var b = a.shift()
if(typeof(b)=="undefined"){
a = arg.slice(0);
b = a.shift()
}
b();
}
</script>
<input type="button" onclick="doNewThings()">```

• The only reason I could conceive of to make a SINGLE button do MULTIPLE actions
would be like a randomized die roll or pick of a card from a deck,
but perhaps the OP will return to show a different reason for the request.

But if the purpose is for a random pick of a value
I could think of easier ways to implement the action.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•