Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to disable the checkboxes

Status
Not open for further replies.

sabavno

Programmer
Joined
Jul 25, 2002
Messages
381
Location
CA
Hi,

On my form I have bunch of checkboxes, they have the same name, but different values.

I want to disable all of them if the first check box that has value "D" is checked.

<input type=&quot;checkbox&quot; name=&quot;RequestType&quot; value=&quot;D&quot;>
<input type=&quot;checkbox&quot; name=&quot;RequestType&quot; value=&quot;S&quot;>
<input type=&quot;checkbox&quot; name=&quot;RequestType&quot; value=&quot;L&quot;>
<input type=&quot;checkbox&quot; name=&quot;RequestType&quot; value=&quot;W&quot;>

Please advise

Thanks.
 
why not use radio buttons instead so this is simplified? _______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
Here it goes...

<input type=&quot;checkbox&quot; name=&quot;RequestType&quot; value=&quot;D&quot; onclick=&quot;document.yourform.RequestType1.disabled=true;document.yourform.RequestType2.disabled=true;document.yourform.RequestType3.disabled=true;&quot;>
<input type=&quot;checkbox&quot; name=&quot;RequestType1&quot; value=&quot;S&quot;>
<input type=&quot;checkbox&quot; name=&quot;RequestType2&quot; value=&quot;L&quot;>
<input type=&quot;checkbox&quot; name=&quot;RequestType3&quot; value=&quot;W&quot;>
 
if you insist [smile]
<html>
<head>
<script language=&quot;JavaScript&quot;>
function dis() {
for (var count=0; count<document.frm.elements.length; count++)
{
var chkelem = document.frm.elements[count];
if(chkelem.checked == false) {
chkelem.disabled = true;
}
}
}
</script>

</head>
<body>
<form name=&quot;frm&quot; action=&quot;&quot; method=&quot;POST&quot; >
<input type=&quot;Checkbox&quot; name=&quot;chk1&quot; onclick=&quot;dis()&quot;>
<input type=&quot;Checkbox&quot; name=&quot;chk2&quot; onclick=&quot;dis()&quot;>
<input type=&quot;Checkbox&quot; name=&quot;chk3&quot; onclick=&quot;dis()&quot;>

</form>
</body>
</html>
_______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
[lol]
that disables everything.
easy fixreplace the if condition with
if((chkelem.type == &quot;checkbox&quot;)&&(chkelem.checked == false)) {
_______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
Thanks for the help

One question though, I have noticed each check box has a different name
On my form they all have the same name, but different values.

What does that matter?
 
names have to be unique in your scripting, if not javascript will give you an error because will not know which element you are trying to manipulate...
 
well, kind of
with checkboxs and radio buttons the names a most commonly the same. if you jsut change the names to be the same you will see the function still works fine. as there is no realtion between the naming of the checkboxs and the function _______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top