473,385 Members | 1,610 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,385 software developers and data experts.

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.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Untitled Document</title>
<script language="javascript">
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="Switch1()"/>
<input type="checkbox" name="check2" onClick="Switch2()"/>
</form>
</body>
</html>

Aug 5 '06 #1
1 1649

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.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Untitled Document</title>
<script language="javascript">
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="toggleCB(this, this.form.cb_02);">cb 01<br>
<input type="checkbox" name="cb_02"
onclick="toggleCB(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
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",...
1
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: ...
11
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...
3
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...
0
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...
2
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...
2
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...
1
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.