473,569 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simplifying disabled checkboxes code

Here is a simple webpage containing two checkboxes.

I have used javascript to diasble one checkbox when the other one is
selected.

At the moment it uses two seperate but identical functions, one for
each checkbox.

I am new to javascript so I don't know but I think it imay be good
practice to simplify this code so only a single funtion is required for
both checkboxes.

I would appreciate any suggestions how to do this and advice if it is
good practice and I would also be grateful for any general feedback on
good practice.

Thanks!

--------------------------------------------------------------------------------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Untitl ed Document</title>
<script language="javas cript">
function Switch1()
{
if (document.form1 .check1.checked == true)
{ document.form1. check2.disabled = true; }
else
{ document.form1. check2.disabled = false; }
}
function Switch2()
{
if (document.form1 .check2.checked == true)
{ document.form1. check1.disabled = true; }
else
{ document.form1. check1.disabled = false; }
}
</script>

</head>
<body>
<form method="post" name="form1">
<input type="checkbox" name="check1" onClick="Switch 1()"/>
<input type="checkbox" name="check2" onClick="Switch 2()"/>
</form>
</body>
</html>

Aug 5 '06 #1
1 1657

Jo************@ gmail.com wrote:
Here is a simple webpage containing two checkboxes.

I have used javascript to diasble one checkbox when the other one is
selected.
The obvious question is why aren't you using radio buttons?

At the moment it uses two seperate but identical functions, one for
each checkbox.

I am new to javascript so I don't know but I think it imay be good
practice to simplify this code so only a single funtion is required for
both checkboxes.

I would appreciate any suggestions how to do this and advice if it is
good practice and I would also be grateful for any general feedback on
good practice.

Thanks!

--------------------------------------------------------------------------------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Untitl ed Document</title>
<script language="javas cript">
The language attribute is deprecated, type is required:

<script type="text/javascript">

function Switch1()
JavaScript already has a 'switch' statement and hence switch is a
reserved word, so while your name isn't actually in conflict with that,
it's very close so use another name...

Try this example:

<script type="text/javascript">

function toggleCB(cb1, cb2){
cb1.disabled = cb2.checked;
cb2.disabled = cb1.checked;
}
</script>

<form action="">
<input type="checkbox" name="cb_01"
onclick="toggle CB(this, this.form.cb_02 );">cb 01<br>
<input type="checkbox" name="cb_02"
onclick="toggle CB(this, this.form.cb_01 );">cb 02<br>
</form>

[...]
--
Rob

Aug 5 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5056
by: Antonio | last post by:
Good Morning, for my application I need to set the disabled color of the checkboxes to blue, how can I do this ??? I know for the combo there is UIManager.put("ComboBox.disabledForeground", new Color(0,0,255)); but seems there is nothing of similar for the checkbox, maybe SUN doesn't think to this ??? Can you help me to ha ve a blue...
1
6203
by: David Wake | last post by:
I have two radio buttons and two checkboxes in a form. I'm trying to write some code so that when a radio button is selected, its corresponding checkbox is disabled. My code looks like this: function radioClicked(index) { document.table_config_form.my_checkbox.disabled = true;
11
3873
by: shree | last post by:
Hello everyone, I'm writing a form which will have a group of checkboxes. When user selects one checkbox, it will automatically disable the rest of the checkboxes in the group. I'm able to develop this function. I also would like to provide a 'clear' button which should trigger via onClick. When 'clear' button is clicked, it should clear...
3
11328
by: Rachel Suddeth | last post by:
I didn't know what to put for a subject, so I just tell you a problem requirement they want for my ap. If the user hovers over a disabled control, they want a text bubble to explain why it's disabled. (This does not seem unreasonable. There are tool tip bubbles coming up on the disabled toolbar buttons on my email reader right now.) Now the...
0
1120
by: Mike Draper | last post by:
The goal of this section of the app is to have a datagrid that displays rows to the user who will then select which rows he wishes to continue the process with. The Grid containing all choices to select from is bound to a datatable with a column that isn't databased, but generated by code with the boolean datatype. When I bind to this...
2
6242
by: Bart Van Hemelen | last post by:
The situation: I have a CheckBoxList cblTest, the items are disabled in cblTest_DataBound in a foreach (ListItem oItem in cblTest.Items) loop. I provide a link that calls a client-side JavaScript that enables the items -- this works perfectly. However, when I then click a LinkButton that does a postback and I loop through the items on...
2
1825
by: BillE | last post by:
Users of a VS2005 asp.net web forms application can complete questionnaires, and when they are completed the questionnaires can be viewed but not modified. Once completed, the controls in the questionnaire are disabled (enabled = false). Many users have complained that the grayed-out appearance makes it difficult to see radio buttons and...
1
1824
by: mikaint | last post by:
I've read that if a field is set to disabled, it will be excluded from the submited fields when sending a form. Is there an alternative way to disable a field but still, send it with the form? I have some checkboxes that have to be disabled to prevent users from changing their status. Readonly won't work with checkboxes, so the only way to...
2
1784
by: CreativeMind | last post by:
hi i have following code but problem is that when i debug both checkboxes have disabled property always false whether it is checked or unchecked..thx for help. function checkFinance(){ var chk1=document.getElementById("_ctl0_ContentPlaceHolder2_chkFinance");
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.