SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to turn on multiple buttons if there are multiple answers?

    Got a bunch of letter buttons in code below:

    Code:
    <?php
        $a = range("A","Z");
    ?>
    
    <table id="answerSection">
        <tr>
    
    <?php
        $i = 1;
        foreach($a as $key => $val){
            if($i%7 == 1) echo"<tr><td>";
            echo"<input type=\"button\" onclick=\"btnclick(this);\" value=\"$val\" id=\"answer".$val."\" name=\"answer".$val."Name\" class=\"answerBtns answers answerBtnsOff\">";      
            if($i%7 == 0) echo"</td></tr>";
            $i++;
        }
    ?>
        </tr>
    </table>
    Now the code below is able to turn on an answer button:

    Code:
    function addwindow(btn) { 
    $('#answer'+btn).addClass("answerBtnsOn");
    }
    But the only problem is that the code above is only able to turn on a single answer button on only. For example if the "Answer" is B, then it will look for button "#answerB" and turn on that button which is button B. or if the "Answer" is E, then it will look for button "#answerE" and turn on that button which is button E.

    The problem is that if there are multiple answers. If the "Answer" is B E, then it does not turn on buttons B and E. This is because it is trying to find button "#answerBE" which is incorrect, it should be looking for button "#answerB" and button "#answerE" and turn them both on.

    Another example is if "Answer" is A D F, it doesn't turn on buttons A D and F because it is trying to find button "#answerADF" which is incorrect, it should be looking for button "#answerA", button "#answerD", and button "#answerF" and turn them all on.

    So my question is that how can I turn on multiple buttons if there are multiple Answers? Do I need to put all the buttons in an array and loop through them so that it is able to go through all the buttons and turn on those buttons which should be turn on?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by carlbrooks View Post
    So my question is that how can I turn on multiple buttons if there are multiple Answers? Do I need to put all the buttons in an array and loop through them so that it is able to go through all the buttons and turn on those buttons which should be turn on?
    Multiple radio buttons cannot be turned on when they have the same name.

    If multiple answers at the same time really are a possibility, you need to use a more appropriate interface, which uses checkboxes instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

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