Jump to content

onclick help


ToddAtWSU

Recommended Posts

I have a grid of radio buttons that only allows one radio button to be selected inside a row and a column. I have a Reset button next to each row to remove the selection from the row and re-enable the rest of the column. I now want to add a Reset All button that clears all the selections and re-enables all the radio buttons. But unfortunately I cannot get my reset all button to work. Here is the snippet of my code showing all the script functionality and the creation of the grid of radio buttons. Thanks for your help!

 

            <script language="javascript">

               // variable to set the number of participants $totalTeams
               var par_num = '<?php global $totalTeams; echo $totalTeams; ?>';

               // variable to set the number of radio buttons per row
               var rad_num = 10;

               function reset_radio(row)
               {
                  // loop through the radio buttons on the row to see which 
                  // radio button is selected

                  for( i = 1 ; i <= rad_num ; i++ )
                  {
                     // if the radio button is selected,
                     // uncheck it and enable the other radio buttons
                     if( document.getElementById( 'par_' + row + '_' + i ).checked )
                     {
                        // unchecking the button
                        document.getElementById( 'par_' + row + '_' + i ).checked = false;
                        // turning the other radion buttons on
                        change_disable_status( i, false );
                        return;
                     }
                  }
               }

               // this function will change the status of the radio buttons
               function change_disable_status(ra, st)
               {
                  for( i = 1 ; i <= par_num ; i++ )
                  {
                     if( !document.getElementById( 'par_' + i + '_' + ra ).checked )
                     {
                        document.getElementById( 'par_' + i + '_' + ra ).disabled = st;
                     }
                  }
               }

               // this function will reset all the radio buttons
               function resetAll( )
               {
                  for( i = 1 ; i <= par_num ; i++ )
                  {
                     for( j = 1 ; j <= rad_num ; j++ )
                     {
                        if( document.getElementById( 'par_' + i + '_' + j ).checked )
                        {
                           document.getElementById( 'par_' + i + '_' + j ).checked = false;
                        }
                        else
                        {
                           document.getElementById( 'par_' + i " '_' + j ).disabled = false;
                        }
                     }
                  }
               }
            </script>
            <table align="center" border="1">
               <tr>
                  <td align="center"> </td>
                  <td align="center">1</td>
                  <td align="center">2</td>
                  <td align="center">3</td>
                  <td align="center">4</td>
                  <td align="center">5</td>
                  <td align="center">6</td>
                  <td align="center">7</td>
                  <td align="center">8</td>
                  <td align="center">9</td>
                  <td align="center">10</td>
               </tr>
               <?php
                  global $totalTeams;
                  global $teams;
                  for( $i = 0 ; $i < $totalTeams ; $i++ )
                  {
                     echo "<tr>";
                     echo "<td align=\"left\">" . $teams[$i] . "</td>";
                     for( $j = 1 ; $j <= 10 ; $j++ )
                     {
                        if( $_POST["par_" . ( $i + 1 )] == "par_" . ( $i + 1 ) . "_" . $j )
                        {
                           echo "<td align=\"center\"><input type=\"radio\" name=\"par_" 
                                . ( $i + 1 ) . "\" id=\"par_" . ( $i + 1 ) . "_" . $j . 
                                "\" value=\"par_" . ( $i + 1 ) . "_" . $j . 
                                "\" onmousedown=\"reset_radio('" . ( $i + 1 ) . 
                                "');\" onclick=\"change_disable_status('" . $j . 
                                "', true);\" checked=\"checked\"/></td>";
                        }
                        else
                        {
                           echo "<td align=\"center\"><input type=\"radio\" name=\"par_" 
                                . ( $i + 1 ) . "\" id=\"par_" . ( $i + 1 ) . "_" . $j . 
                                "\" value=\"par_" . ( $i + 1 ) . "_" . $j . 
                                "\" onmousedown=\"reset_radio('" . ( $i + 1 ) . 
                                "');\" onclick=\"change_disable_status('" . $j . 
                                "', true);\" /></td>";
                        }
                     }
                     echo "<td align=\"center\"><input type=\"button\" value=\"Reset\" onclick=\"reset_radio('" . ( $i + 1 ) . 
                          "'); return false;\"/></td>";
                     echo "</tr>";
                  }
                  for( $i = 0 ; $i < $totalTeams ; $i++ )
                  {
                     for( $j = 1 ; $j < 11 ; $j++ )
                     {
                        if( $_POST["par_" . ( $i + 1 )] == "par_" . ( $i + 1 ) . "_" . $j )
                        {
                           echo "<script language=\"javascript\"> change_disable_status('" . $j . "', true ); </script>";
                        }
                     }
                  }
               ?>
            </table>
            
            <p align="center">
               <input type="button" value="Reset All" onclick="resetAll( );"/>
            </p>

Link to comment
Share on other sites

You should probably pass the number or rows there are to the function. That is all I can think to make it work.

 

 

Just to let you know, most poeple won't read through all of you script to find your problem. Next time you should probably post just the code that needs work.

Link to comment
Share on other sites

The variable par_num is the number of rows there are and the variable rad_num is the number of columns in the grid of radio buttons. So I don't know how passing in the number of rows would be of any help. Here is the reduced version of the code I think is necessary.

 

<script language="javascript">

               // variable to set the number of participants $totalTeams
               var par_num = '<?php global $totalTeams; echo $totalTeams; ?>';

               // variable to set the number of radio buttons per row
               var rad_num = 10;

               // this function will reset all the radio buttons
               function resetAll( )
               {
                  for( i = 1 ; i <= par_num ; i++ )
                  {
                     for( j = 1 ; j <= rad_num ; j++ )
                     {
                        if( document.getElementById( 'par_' + i + '_' + j ).checked )
                        {
                           document.getElementById( 'par_' + i + '_' + j ).checked = false;
                        }
                        else
                        {
                           document.getElementById( 'par_' + i " '_' + j ).disabled = false;
                        }
                     }
                  }
               }

 

<p align="center">
               <input type="button" value="Reset All" onclick="resetAll( );"/>
            </p>

 

Thanks again for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.