0

so i am trying to make dynamic checkboxs that will show/hide certain content when clicked. I have this but cannot get it to work properly:

<html> 
<head> 
    <title>Checkbox Event Handler</title> 
    <style type="text/css"> 
        #myGroup {visibility:hidden} 
    </style> 
    <script type="text/javascript"> 
        function toggle(chkbox, group) { 
            var visSetting = (chkbox.checked) ? "visible" : "hidden"; 
            document.getElementById(group).style.visibility = visSetting; 
        } 
        function swap(radBtn, group) { 
            var modemsVisSetting = (group == "modems") ? ((radBtn.checked) ? "" : "none") : "none"; 
            document.getElementById("modems").style.display = modemsVisSetting; 
        } 
    </script>
    <?php require_once("/var/www/html/exercise/Task/functions.php"); ?>
</head> 
<body> 
    <?php
        $seqA[]="AAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBB";
        $seqA[]="BBBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCDDDDDDDDDD0";
        $seqA[]="CCCCCCCCCCCCCCCCCCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";
    ?>
    <form>
    <?php
        $i=o;
        foreach($seqA as $seq){
    ?>
            <input type="checkbox" name="monitor"onclick="toggle(this, 'myGroup')" />show results
            <span id="myGroup">
     <?php
            $score=rawtransform(950);
            $truecol= getcolor($score,220);
            colorSequence($seq,5/*hit*/,$truecol,4);
        }
      ?>
            </span>
    </form>
</body>
</html>

it opens the first string in $seqA normally and works fine however the second checkbox is within the first string ? Im sure ive done something very stupid, but im new to programming. Anyone help please ?

2 Answers 2

1

The problem I see is that in each foreach loop iteration you add element span with the same id attribute. Set unique IDs, change your JavaScript toggle function to address specified span and it will work.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not very familiar with PHP, but it appears that you are generated multiple span elements with the same ID, which would cause the document.getElementById to act in unpredictable ways.

3 Comments

Not unpredictable. It chooses the first one.
@keune true, but now you are imposing order dependency on your elements, not to mention your HTML is invalid.
@jbaey no, i just stated that when there are multiple elements with the same id, document.getElementById returns the first one. I agree that it's totally bad practise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.