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 6 of 6
  1. #1
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts

    boolean value for a button?

    hi,

    I'm trying to modify this function:

    Code:
    function togglePoly(poly_num) {
            if (document.getElementById(poly_num)) {
               if (document.getElementById(poly_num).checked) {
                  gpolys[poly_num].show();
    			} else {
                  gpolys[poly_num].hide();
    			}
            }
          }
    which works for a checkbox to one that works for a button.

    I guess I'm stuck on what to replace "checked" with there in the 3rd line.

    is there some one word answer, or do I need to I rewrite the function to be more button-friendly?

    thanks in advance

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    ??? So how will you inform the USER what the state of the button is? Change the value in the button? Use an image and change the image?

    WORK WITH US!!!

    SHOW your code for the actual 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.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    sorry, didn't realise that was necessary. the button fires the function:

    Code:
    sidebar_html += '<input type="button" onclick="togglePoly('+poly_num+')" value="Go!"<br />';
    so I guess the user will know the state because the function will have fired (?)

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>TEST</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <p>
    <script type="text/javascript">
    
    gpolys=[];
    
    gpolys['nuts'] = 
    { 
     show:function(){ alert('Show'); }, 
     
     hide:function(){ alert('Hide'); } 
    }
    
    function togglePoly( poly_num ) 
    {
     var btn = document.getElementById(poly_num);
     
     gpolys[poly_num][ ( btn.checked ^= true ) ? 'show' : 'hide' ]();
    }
    </script>
    
    <input type=button id='nuts' value = 'Go' onclick='togglePoly( this.id )'>
    
    </body>
    </html>

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    REWRITE OF THIS MESSAGE: Are you suggesting that he simply add a "checked" attribute to his buttons?

    Okay, I guess that works.
    Last edited by Old Pedant; 12-07-2010 at 09:29 PM.
    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    This is a bit of a stab in the dark, but here goes:
    Code:
    function togglePoly(poly_num) {
            var seg = gpolys[poly_num];
            if ( seg.style.visibility == "visible" ) seg.hide(); else seg.show();
    }
    If the hide/show code is using style.display, instead, then try
    Code:
    function togglePoly(poly_num) {
            var seg = gpolys[poly_num];
            if ( seg.style.display != "none" ) seg.hide(); else seg.show();
    }
    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.


  •  

    Posting Permissions

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