Advertisement
lalatino

clickable grouped buttons

Jul 8th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <title> http://stackoverflow.com/questions/11382453/how-to-turn-on-multiple-buttons/11382925 </title>
  4. <style>
  5. .answerBtnsOff { }
  6. .answerBtnsOn { border-color:red; border-radius:2px; color:red; }
  7. </style>
  8.  
  9. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  10. <script type="text/javascript">
  11.  
  12. <?php
  13.     $a = range("A","Z");
  14.     $correct = "ADEIRW";  // some string with correct letters (aka answers)
  15. ?>
  16.  
  17.     var oanswers = new Object();
  18.     var answers = '<?php echo $correct; ?>'; // this object's attributes are named as correct letters
  19.     for (var i=0; i<answers.length; i++) {
  20.         oanswers[answers[i]] = true;
  21.     }
  22.    
  23.     function btnclick(btn) {
  24.         if (!oanswers.hasOwnProperty(btn.id[6])) {
  25.             return;
  26.         }
  27.         if ($(btn).hasClass('answerBtnsOff')) {
  28.             $('.answers').each(function(){ // turn all correct answers on
  29.                 if (oanswers.hasOwnProperty(this.id[6])) {
  30.                     $(this).addClass('answerBtnsOn');
  31.                     $(this).removeClass('answerBtnsOff');
  32.                 }
  33.             });
  34.         } else {
  35.             $('.answers').each(function(){ // turn all correct answers off again
  36.                 if (oanswers.hasOwnProperty(this.id[6])) {
  37.                     $(this).addClass('answerBtnsOff');
  38.                     $(this).removeClass('answerBtnsOn');
  39.                 }
  40.             });
  41.         }
  42.     }
  43.  
  44.  
  45. </script>
  46.  
  47. </head>
  48. <body>
  49.  
  50. <table id="answerSection">
  51.     <tr>
  52. <?php
  53.     $i = 1;
  54.     foreach($a as $key => $val){
  55.         if($i%7 == 1) echo"<tr><td>";
  56.         echo '<input type="button" onclick="btnclick(this);" value="'.$val.'" id="answer'.$val.'" name="answer'.$val.'Name" class="answerBtns answers answerBtnsOff">';
  57.         if($i%7 == 0) echo"</td></tr>";
  58.         $i++;
  59.     }
  60. ?>
  61.     </tr>
  62. </table>
  63.  
  64. </body>
  65. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement