J
Joseph.Moffatt
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>
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>