Jump to content

Check boxes script help


Darkmatter5

Recommended Posts

I have 5 check boxes and I need for if a specific one is checked all the others uncheck.  I see on a lot of sites a button that when clicked checks all boxes, but I would like for a checkbox being checked to activate this script of unchecking the other check boxes.  How can I do this?

Link to comment
https://forums.phpfreaks.com/topic/133373-check-boxes-script-help/
Share on other sites

Do you mean like

 

(checkbox) Yes I would like to subscribe to all of these

 

    (checkbox) magazine 1

    (checkbox) magazine 2

    (checkbox) magazine 3

    (checkbox) magazine 4

 

and when they check Yes I would like to subscribe to all of these then it will check/uncheck all of those?

 

To do that you use this code

 

<script type="text/javascript">
function radio()
{
      if(document.form1.radio.checked == false)
      {
            document.form1.radio.checked = true;
      }
      else (document.form1.radio.checked == true)
      {
            document.form1.radio.checked = false;
      }
}
</script>

 

He asked to have all the other field unchecked if the particular field was checked. This will do that, as well as disable the fields when unchecked.

 

<html>
<head>
<script type="text/javascript">

function uncheckAll(uncheckAll_switch)
{
    //Uncheck all boxes if option checked
    if (uncheckAll_switch)
    {
        document.getElementById('check1').checked = false;
        document.getElementById('check2').checked = false;
        document.getElementById('check3').checked = false;
        document.getElementById('check4').checked = false;
    }

    //Disable/Enable all the checkboxes
    document.getElementById('check1').disabled = uncheckAll_switch;
    document.getElementById('check2').disabled = uncheckAll_switch;
    document.getElementById('check3').disabled = uncheckAll_switch;
    document.getElementById('check4').disabled = uncheckAll_switch;

    return;
}

</script>
</head>
<body>

<input type="checkbox" name="clear_all" id="clear_all" onclick="uncheckAll(this.checked);">
Check here to uncheck all other boxes
<br><br>
<input type="checkbox" name="check1" id="check1"> Checkbox 1<br>
<input type="checkbox" name="check2" id="check2"> Checkbox 2<br>
<input type="checkbox" name="check3" id="check3"> Checkbox 3<br>
<input type="checkbox" name="check4" id="check4"> Checkbox 4<br>

</body>
</html>

Oh well thats why I asked if thats what he was asking before posting my code.

Actually I misread your response. Your code does uncheck the "child" field when the "parent" is checked. But, it also checks the "child" when the "parent" is unchecked. Not sure if that was what he wanted. But, then again, not enough specifics were given.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.