473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.
Oct 25 '10 #1
3 17095
Dormilich
8,658 Expert Mod 8TB
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.
Oct 25 '10 #2
Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="checkall" id="checkall" onclick="check_all()">Select All
  2. <br>
  3.  
  4. <input type="checkbox" name="check_0" id="check_0">Option 1
  5. <input type="checkbox" name="check_1" id="check_1">Option 2
  6. <input type="checkbox" name="check_2" id="check_2">Option 3
  7. <input type="checkbox" name="check_3" id="check_3">Option 4
  8. <input type="checkbox" name="check_4" id="check_4">Option 5
  9. <input type="checkbox" name="check_5" id="check_5">Option 6
  10. <input type="checkbox" name="check_6" id="check_6">Option 7
  11. <input type="checkbox" name="check_7" id="check_7">Option 8
  12. <input type="checkbox" name="check_8" id="check_8">Option 9
  13. <input type="checkbox" name="check_9" id="check_9">Option 10
  14.  
  15. <br><hr>
  16.  
  17. <script type="text/javascript">
  18. function check_all()
  19. {
  20.  
  21.     for(i=0;i<10;i++)
  22.     {
  23.         tmp_checkbox_id = "check_"+i;
  24.             //alert(tmp_checkbox_id);
  25.             if(document.getElementById("checkall").checked == true)
  26.             {    
  27.                 //alert("hi");
  28.                 document.getElementById(tmp_checkbox_id).checked = true;
  29.             }
  30.             else
  31.             {    
  32.                 //alert("bye");
  33.                 document.getElementById(tmp_checkbox_id).checked = false;
  34.             }
  35.  
  36.     }
  37. }
  38. </script>
May 17 '12 #3
Nicodemas
164 Expert 100+
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.


Expand|Select|Wrap|Line Numbers
  1. <!doctype html>
  2. <html>
  3.  
  4. <head>
  5. <script type="text/javascript">
  6.  
  7. function DisableAll(elementID){
  8.   var _container = document.getElementById(elementID);
  9.   var _chks = _container.getElementsByTagName("INPUT");
  10.   var _numChks = _chks.length - 1;
  11.  
  12.   for(var i = 0; i <= _numChks; i++){
  13.     _type = _chks[i].getAttribute("type");
  14.  
  15.     if(_type=="checkbox")
  16.       _chks[i].disabled=true;
  17.   }
  18. }
  19. </script>
  20. </head>
  21.  
  22. <body>
  23.  
  24. <button type="button" onclick="DisableAll('MyForm')">Show Me</button>
  25.  
  26. <form id="MyForm">
  27.  <input type="checkbox" name="check_0" id="check_0">Option 1
  28.  <input type="checkbox" name="check_1" id="check_1">Option 2
  29.  <input type="checkbox" name="check_2" id="check_2">Option 3
  30.  <input type="checkbox" name="check_3" id="check_3">Option 4
  31.  <input type="checkbox" name="check_4" id="check_4">Option 5
  32.  <input type="checkbox" name="check_5" id="check_5">Option 6
  33.  <input type="checkbox" name="check_6" id="check_6">Option 7
  34.  <input type="checkbox" name="check_7" id="check_7">Option 8
  35.  <input type="checkbox" name="check_8" id="check_8">Option 9
  36.  <input type="checkbox" name="check_9" id="check_9">Option 10 
  37. </form>
  38.  
  39. </body>
  40.  
  41. </html>
May 20 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Maggie Eusebio Schock | last post by:
Hi, I would like to understand when a selected cell, selected row or selected rows in the DataGrid control ends up on the clipboard. All I know is that the selection ends up on the clipboard,...
0
by: sam | last post by:
I have 2 radio buttons on my form which are hidden. I need to have these radio buttons visible once a checkbox is selected. The radio buttons are hidden on loading of the form but I cannot make them...
1
by: Charles Shao | last post by:
How to chang bgcolor of row when mouse selected a row? I have a HTML table in the page. I hope to change <tr.bgcolor> when user clicks on a column, just like user has selected this row. How...
2
by: Alex Smith | last post by:
Hi Friends, How can I check a checkbox selected more than one times in DataGrid ? My First Priority is check with Client Side(Java Script), if it is not easy than check server side (C#). Thanks...
4
by: Matrixreloadedth | last post by:
How to change disable color of Checkbox??? I have a checkbox with forecolor in red but when i disable by set Enable properties to false forecolor is changed to gray color but i don't want it. how...
6
by: Tafi | last post by:
Please help. I have, say, 10 checkboxes on a form. I would like the form to disable, say checkbox 7 and 8 if I select checkbox 1. How do I achieve this.
0
by: prpleprncs | last post by:
I have a form that has a five checkboxes. Two of the checkboxes (let's call them box4 and box5) cannot both be checked. For example, if box4 is checked, box5 will uncheck and vice versa. I am new...
1
by: Anonymous | last post by:
Which is the correct approach to disable buttons when leaving an aspnet page? Currently I have the following html, which disables buttons when pressing F5 in FireFox, which ofcourse can occur...
5
by: hidayu1986 | last post by:
how to disable textbox when click on radiobutton using VB script on Visual Studio.Net 2005. currently i used this coding but it is not functioning. Private sub...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.