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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling radio button and fields from other radio

Status
Not open for further replies.

itsoookkk

MIS
Dec 26, 2005
5
PK
Hi, I want to diable form fields including some radio buttons on the selection of one radio button , I have found a javascript from net and tried to change it according to my requirement , but although after the slection of "No" from first radio it show the other fields disabled, but one can still enter value in them , how it can work ? In my form i need one radion dependining on othrt , that radio depending on another , and its like a chain , slecting a no will break the chain,
Please find teh code below and guide mr that how can i do that,
Thanks In Advance

<HTML>
<HEAD>
<SCRIPT>




function yes() {
MMDiv.style.color ='black';
mainform.MakeModel.disabled=false;
mainform.MakeModel.focus();
mainform.P.disabled=false;
mainform.P.focus();
mainform.PP.disabled=false;
mainform.PP.focus();
}
function yes1() {
MMDiv.style.color ='black';
mainform.p.disabled=false;
mainform.p.focus();

}
function no() {
MMDiv.style.color ='gray';
mainform.MakeModel.disabled=true;
mainform.MakeModel.value='';
mainform.p.disabled=true;
mainform.p.value='';
mainform.pp.disabled=true;
mainform.pp.value='';
}
function no1() {
MMDiv.style.color ='gray';
mainform.pp.disabled=true;
mainform.pp.value='';

}



</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="mainform">
Allow Entry In all the fields Below
<INPUT TYPE="RADIO" NAME="PRINTER" onclick="yes();">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Don't Allow Entry in any of the fields below ( Also disable Radio button)
<INPUT TYPE="RADIO" NAME="PRINTER" onclick="no();" >
<p>
<BR>
</p>
<DIV ID="MMDiv" style="color:gray">
<LABEL FOR="MakeModel">
<INPUT TYPE="TEXT" ID="MakeModel" DISABLED size="20">
Make and Model</LABEL><p>&nbsp;</p>
<p>&nbsp;<INPUT TYPE="RADIO" NAME="P" onclick="yes1();">Allow Entry in radio
button below
<INPUT TYPE="RADIO" NAME="P" onclick="no1();" >Disable the radio buttons below
<p>&nbsp;<INPUT TYPE="RADIO" NAME="PP" >Yes
<INPUT TYPE="RADIO" NAME="PP" >No
</DIV>
<BR>
<INPUT TYPE="SUBMIT">
<INPUT TYPE="RESET">
</FORM>
</BODY>
</HTML>
 
Let's start simple.

- Specify a type for your scripting language:

Code:
<script [!]type="text/javascript"[/!]>

Don't use lazy / sloppy methods for accessing form fields:

Code:
mainform.p.

use this syntax instead:

Code:
document.forms['mainform'].elements['p'].


Tidy your code up. Indent it. Make it look good. Tidy code leads to better readability, more structured thinking, and aids anyone trying to help you.

Once you've done this, I suggest reposting your new tidy code inside TGML [ignore]
Code:
[/ignore] tags to further aid readability (see the "Process TGML" link below the post box for more information on this).

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
When I click the radio button with the title "Don't Allow Entry in any of the fields below ( Also disable Radio button)" I get a javascript error reported in Firefox:
Error: mainform.p has no properties
This error would prevent the rest of the script from working... so the first step is to fix that.

I would suggest you change the name of your radio button to be something other than "P". If I change it to something else (a little more descriptive) then it works fine for me.

Using Firefox as a debugging tool is very useful for javascript development.

Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Those radio buttons should be referred to.
[tt]
[red]document.[/red]mainform.[red]P[/red][red][idx][/red]
[red]document.[/red]mainform.[red]PP[/red][red][idx][/red]
[/tt]
where idx being (0,1,...,length of P's or PP's). If for all of them, use idx as the looping index.

 
With the figurative ranges, I sure meant (0,1,...,length of P's or PP's [red]less one[/red]).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top