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 4 of 4
  1. #1
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,102
    Thanks
    56
    Thanked 576 Times in 572 Posts

    change value of all buttons after onclick

    Hi,

    I have a row of buttons that are made by innerhtml using this code:


    Code:
     sidebar_html += '<div id="button"><input type="button" onclick="toggleRun('+poly_num+')" value="Start Bus" id="runBtn'+poly_num+'" style="width:120px; height:24px; text-align:center"></div><br />';
    to fire this function:

    Code:
    function toggleRun(poly_num){
     var btn = document.getElementById('runBtn'+poly_num);
      if (document.getElementById('runBtn'+poly_num).onclick) {	
      if (run){
    		run=false;
    		document.getElementById('runBtn'+poly_num).value="Reload Page";
    		startAnimation(poly_num);
    		} else {
    		run=true;
    		document.getElementById('runBtn'+poly_num).value="Reload Page";
    		window.location.reload();
    		} 
    }
    }
    the function can only run once, so the idea is that the button either runs it the first time or reloads the page.

    at the moment, the way it's working is that the button that's clicked changes its value to "Reload Page" but the others keep their original value, even though it reloads if you click them.

    So I'd like a way to change the value of the rest of the buttons when one is clicked. Is that possible?

    Here's the page if I didn't explain myself well enough.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,411
    Thanks
    85
    Thanked 4,907 Times in 4,868 Posts
    Code:
    function toggleRun(poly_num)
    {
        var btn = document.getElementById('runBtn'+poly_num);
        if ( btn.onclick) 
        {    
            var inps = document.getElementsByTagName("input");
            for ( var i = 0; i < inps.length; ++i )
            {
                var inp = inps[i];
                if ( inp.value = "Start Bus" ) inp.value = "Reload Page";
            }
    
            if (run)
            {
                run=false;
                startAnimation(poly_num);
            } else {
                run=true;
                window.location.reload();
           } 
       }
    }
    ???? Untested, but makes sense.
    Be yourself. No one else is as qualified.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,411
    Thanks
    85
    Thanked 4,907 Times in 4,868 Posts
    But the code seems overly complex.

    Code:
    sidebar_html += 
        '<div id="button">'
      + '<input type="button" onclick="toggleRun(this,' + poly_num + ')"
      + ' value="Start Bus" '
      + ' style="width:120px; height:24px; text-align:center">'
      + '</div><br />';
    
    ...
    function toggleRun(btn, pnum)
    {
        var inps = document.getElementsByTagName("input");
        for ( var i = 0; i < inps.length; ++i )
        {
            var inp = inps[i];
            if ( inp.value = "Start Bus" ) inp.value = "Reload Page";
        }
     
       if (run)
       {
            run=false;
            startAnimation(pnum);
       } else {
            run=true;
            window.location.reload();
       } 
    }
    Unless you need the id's on the buttons for some other reason, get rid of them. Pass this along with the number. And what's the point of looking for the onclick property??? How else could you *get* to the function other than by a click on a button??
    Be yourself. No one else is as qualified.

  • Users who have thanked Old Pedant for this post:

    xelawho (12-08-2010)

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,102
    Thanks
    56
    Thanked 576 Times in 572 Posts
    Quote Originally Posted by Old Pedant View Post
    But the code seems overly complex.
    Well you know what they say- it takes someone smart to make something simple.

    Thanks once again, Old Pedant. You missed an apostrophe on the third line of the sidebar_html there, but I guess I'll let that slide


  •  

    Posting Permissions

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