...

View Full Version : Button do new thing each time.



sherlockturtle
07-31-2011, 04:46 PM
How could a make a html button that each time your press it it does a new things?

Philip M
07-31-2011, 05:03 PM
No idea what you mean, but perhaps:-


<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

jmrker
07-31-2011, 05:11 PM
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


<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
}

DaveyErwin
07-31-2011, 05:59 PM
Here is a different approach ...


<script>
(function(a){
doNewThings = function(){
var b = a.shift()
if(typeof(b)=="undefined")alert("all done")
else b();
}
})([function(){alert("firstThing")},
function(){alert("secondThing")},
function(){alert("thirdThing")}])
</script>
<input type="button" onclick="doNewThings()">

Philip M
07-31-2011, 07:23 PM
Or perhaps


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? :confused:

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

jmrker
08-01-2011, 03:31 AM
...

jmrker - count = (count++ % 4); // untested - does not work (yields 0 always and count is never incremented)
:thumbsup:
Thanks ... it was just a shot off the top ... good to remember to NOT be so fancy at times. :o

Old Pedant
08-01-2011, 05:40 AM
Or how about:



<script>
function one() { alert("one"); }
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.

DaveyErwin
08-01-2011, 06:23 AM
Or how about:



<script>
function one() { alert("one"); }
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 ....



<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();
}
})([function(){alert("firstThing")},
function(){alert("secondThing")},
function(){alert("thirdThing")}])
</script>
<input type="button" onclick="doNewThings()">

jmrker
08-01-2011, 04:15 PM
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.
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum