How to disable all checkboxes when one checkbox is selected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BaseballGraphs
    New Member
    • Sep 2010
    • 75

    How to disable all checkboxes when one checkbox is selected

    Hello,

    I am looking to disable all checkboxes when one checkbox is selected. I cannot use radio buttons, so I need to be able to disable the checkboxes if 1 checkbox has been selected.

    Can you please assist if you know how to perform this sort of function.

    Thanks very much.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    why can’t you use radio buttons? it is easier to make a radio button deselectable than make checkboxes like radiobuttons.

    if that’s still not an option, deselect all checkboxes onclick and recheck the current one.

    Comment

    • Bharat383
      New Member
      • Aug 2011
      • 93

      #3
      Code:
      <input type="checkbox" name="checkall" id="checkall" onclick="check_all()">Select All
      <br>
      
      <input type="checkbox" name="check_0" id="check_0">Option 1
      <input type="checkbox" name="check_1" id="check_1">Option 2
      <input type="checkbox" name="check_2" id="check_2">Option 3
      <input type="checkbox" name="check_3" id="check_3">Option 4
      <input type="checkbox" name="check_4" id="check_4">Option 5
      <input type="checkbox" name="check_5" id="check_5">Option 6
      <input type="checkbox" name="check_6" id="check_6">Option 7
      <input type="checkbox" name="check_7" id="check_7">Option 8
      <input type="checkbox" name="check_8" id="check_8">Option 9
      <input type="checkbox" name="check_9" id="check_9">Option 10
      
      <br><hr>
      
      <script type="text/javascript">
      function check_all()
      {
      
      	for(i=0;i<10;i++)
      	{
      		tmp_checkbox_id = "check_"+i;
      			//alert(tmp_checkbox_id);
      			if(document.getElementById("checkall").checked == true)
      			{	
      				//alert("hi");
      				document.getElementById(tmp_checkbox_id).checked = true;
      			}
      			else
      			{	
      				//alert("bye");
      				document.getElementById(tmp_checkbox_id).checked = false;
      			}
      
      	}
      }
      </script>
      Last edited by acoder; May 23 '12, 04:20 PM.

      Comment

      • Nicodemas
        Recognized Expert New Member
        • Nov 2007
        • 164

        #4
        A cleaner, more dynamic solution.

        When the button is clicked, all checkboxes within the parent element passed into the function are disabled. Doesn't rely on static IDs are specific number of checkboxes.


        Code:
        <!doctype html>
        <html>
        
        <head>
        <script type="text/javascript">
        
        function DisableAll(elementID){
          var _container = document.getElementById(elementID);
          var _chks = _container.getElementsByTagName("INPUT");
          var _numChks = _chks.length - 1;
        
          for(var i = 0; i <= _numChks; i++){
            _type = _chks[i].getAttribute("type");
        
            if(_type=="checkbox")
              _chks[i].disabled=true;
          }
        }
        </script>
        </head>
        
        <body>
        
        <button type="button" onclick="DisableAll('MyForm')">Show Me</button>
        
        <form id="MyForm">
         <input type="checkbox" name="check_0" id="check_0">Option 1
         <input type="checkbox" name="check_1" id="check_1">Option 2
         <input type="checkbox" name="check_2" id="check_2">Option 3
         <input type="checkbox" name="check_3" id="check_3">Option 4
         <input type="checkbox" name="check_4" id="check_4">Option 5
         <input type="checkbox" name="check_5" id="check_5">Option 6
         <input type="checkbox" name="check_6" id="check_6">Option 7
         <input type="checkbox" name="check_7" id="check_7">Option 8
         <input type="checkbox" name="check_8" id="check_8">Option 9
         <input type="checkbox" name="check_9" id="check_9">Option 10 
        </form>
        
        </body>
        
        </html>

        Comment

        Working...