Results 1 to 5 of 5

Thread: display hidden div when unchecking checkbox

  1. #1
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default display hidden div when unchecking checkbox

    hi all

    i have this script but it does not display the div id when unchecking the yep check box, why?

    <td>
    <div id="radiobuttons" style="display:;">
    <input type='radio' name='zoekterm' value='100'>100-nummers<br>
    <input type='radio' name='zoekterm' value='500'>500-nummers<br>
    <input type='radio' name='zoekterm' value='beide' checked>Alle
    </div><br>
    </td>
    <td>
    <input type="checkbox" name="nope" onclick="toggleonRadios()" checked>check<br>
    <input type="checkbox" name="yep" onclick="toggleRadios()">check<br>
    <input type="checkbox" name="yep" onclick="toggleRadios()">check<br>

    <input type="checkbox" name="yep" onclick="toggleRadios()">check<br>
    </td>
    <script type="text/javascript">
    var radioDiv = document.getElementById("radiobuttons");
    function toggleonRadios(){
    if(radioDiv.style.display = "none"){
    radioDiv.style.display == "";
    }
    }
    function toggleRadios(){
    if(radioDiv.style.display == ""){
    radioDiv.style.display = "none";
    }
    }

    </script>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Your three checkboxes named 'yep' all have onclick="toggleRadios()". If you look in that function, it only says to set the display property of the div with the id of radiobuttons to a value of 'none' and only if it is already set to nothing. If you want the function to actually toggle, it needs to say:

    Code:
    function toggleRadios(){
    if(radioDiv.style.display == "")
    radioDiv.style.display = "none";
    else
    radioDiv.style.display = "";
    }
    That still will not guarentee that the checked state of the checkbox wil have anything to do with it.

    I'd leave the function the way it is, use only one yep checkbox and do something like this:

    HTML Code:
    <input type="checkbox" name="yep" onclick="if (this.checked){toggleonRadios()}else{toggleRadios()}">check
    But, for that to work, fix the toggleonRadios function:

    Code:
    function toggleonRadios(){
    if(radioDiv.style.display = "none"){
    radioDiv.style.display = "";
    }
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi John

    Thanks for your help, I appreciate it...I don't think I was clear enough as it still does not work the way I want.

    The nope box should not do anything to div id but as soon as one at leat of the 3 yep boxes is checked then the div id gets hidden. On the other hand, if the none of the yep is checked then I want to show div id again.

    Cheers
    Jack

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, I did answer your question though, do you think you can take it from here? One other thing I missed though is that after I partly fixed toggleonRadios() to:

    Code:
    function toggleonRadios(){
    if(radioDiv.style.display = "none"){
    radioDiv.style.display = "";
    }
    I realize that the conditional needs two equal signs, it should be:

    Code:
    function toggleonRadios(){
    if(radioDiv.style.display == "none"){
    radioDiv.style.display = "";
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I played around a bit more and this does what you say:

    Quote Originally Posted by surboomer
    The nope box should not do anything to div id but as soon as one at leat of the 3 yep boxes is checked then the div id gets hidden. On the other hand, if the none of the yep is checked then I want to show div id again.
    Code:
    <td>
    <div id="radiobuttons" style="display:;">
    <input type='radio' name='zoekterm' value='100'>100-nummers<br>
    <input type='radio' name='zoekterm' value='500'>500-nummers<br>
    <input type='radio' name='zoekterm' value='beide' checked>Alle
    </div><br> 
    </td>
    <td>
    <!--"The nope box should not do anything to div id"-->
    <input type="checkbox" name="nope" checked>check<br>
    <input type="checkbox" name="yep" onclick="if (this.checked){toggleRadios()}else{toggleonRadios()}">check<br>
    <input type="checkbox" name="yep" onclick="if (this.checked){toggleRadios()}else{toggleonRadios()}">check<br>
    <input type="checkbox" name="yep" onclick="if (this.checked){toggleRadios()}else{toggleonRadios()}">check<br>
    </td>
    <script type="text/javascript">
    var radioDiv = document.getElementById("radiobuttons");
    function toggleonRadios(){
    var yeps=document.getElementsByTagName('input')
    for (var i_tem = 0; i_tem < yeps.length; i_tem++)
    if (yeps[i_tem].name=='yep'&&yeps[i_tem].checked)
    return;  // exits here before restoring the div unless all yeps are unchecked
    if(radioDiv.style.display = "none"){
    radioDiv.style.display = "";
    }
    }
    function toggleRadios(){
    if(radioDiv.style.display == ""){
    radioDiv.style.display = "none";
    }
    }
    
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •