Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-08-2010, 10:16 PM   PM User | #1
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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.
xelawho is offline   Reply With Quote
Old 12-08-2010, 11:20 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-08-2010, 11:25 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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??
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
xelawho (12-08-2010)
Old 12-08-2010, 11:48 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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
xelawho is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:37 AM.


Advertisement
Log in to turn off these ads.