Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post

    Button do new thing each time.

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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,247
    Thanks
    203
    Thanked 2,556 Times in 2,534 Posts
    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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Lightbulb

    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
    }

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Here is a different approach ...

    Code:
    <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()">

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,247
    Thanks
    203
    Thanked 2,556 Times in 2,534 Posts
    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)
    Last edited by Philip M; 07-31-2011 at 07:32 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Arrow

    Quote Originally Posted by Philip M View Post
    ...

    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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,169
    Thanks
    80
    Thanked 4,560 Times in 4,524 Posts
    Or how about:

    Code:
    <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.

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by Old Pedant View Post
    Or how about:

    Code:
    <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 ....

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

  • #9
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts
    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
    •