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 TouchToneTommy 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 1

Status
Not open for further replies.

imarosel

Technical User
Jun 23, 2005
149
US
Does anyone know how I can disable the following radio button?

<input name="valname" type="radio" <%=val1%>value="val1">val1&nbsp;&nbsp;
<input name="valname" type="radio" <%=val2%>value="val2">val2&nbsp;&nbsp;

I have code that loops through other inputs by input name and disables all those fine

<script language='JavaScript'>document.forms.formname.fieldname.disabled=true;</script>

this doesn't seem to work for the radio button.
 
they both have the same name. if you are looping through form fields, these are in a collection.

you will need to loop through the collection of radio buttons and disable each.

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
or, here's a better example:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]

<html>
<head>
<title>Untitled</title>

<script type="text/javascript"><!--

function disableAll(f) {
    var e = f.elements;
    for ( var i = 0; i < e.length; i++ ) {
        e[i].disabled = true;
    }
}

//--></script>

</head>

<body onload="disableAll(document.forms['f']);">

<form name="f">
  <input type="text" name="t1" />
  <input type="text" name="t2" />
  <input type="text" name="t2" />
  <select name="s1"></select>
  <input type="radio" name="r1" />
  <input type="radio" name="r1" />
  <input type="radio" name="r2" />
</form>

</body>
</html>

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
Ok, I'm not trying to disable everything on the form just some inputs from a database that I loop through. What I gather from what you say is that if I gave each radio button a different name my method would work?
 
you don't want to do that. that's not the point of a radio button.

show what code you're using (view > source).

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
Ok, I'm not trying to disable everything on the form just some inputs from a database that I loop through.

In which case, the code that cLFlaVA gave should work just fine with a small tweak - simply use the loop code to loop around a specifically-named set of inputs rather than all inputs in a form.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
the code I posted originally is what I use
<script language='JavaScript'>document.forms.formname.[highlight]fieldname[/highlight].disabled=true;</script>

I just use a loop to loop through fieldname. All the fieldnames are looped through just fine except for the radio buttons, so far my understanding is this is because both radio button inputs have the same name. Is there something wrong with my radio button code?

<input name="valname" type="radio" <%=val1%>value="val1">val1&nbsp;&nbsp;
<input name="valname" type="radio" <%=val2%>value="val2">val2&nbsp;&nbsp;

I would think it would be possible to disable the radio button knowing the input name of it, but if not a method of disabling all radio buttons could work.
 
When you have more than one radio button with the same name, you can reference them as if they were an array. Instead of :

document.form1.myRadio.disabled = true;

...you might say:

document.form1.myRadio[0].disabled = true;

...to disable the first radio button that appears in the HTML. The "array" is zero-based.

'hope that helps.

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Dave,

That works pretty good so far.

To keep me from hard coding all my radio buttons, does anyone have an example of how I can check the input to see if it's a radio and then count the elements in the radio button array?
 
imarosel,

I think you'd get a better answer if you described, in more specific terms, what exactly it is you're doing, but I will try to answer.

Here's a code snippet that might help:

Code:
var inputObjs = document.getElementsByTagName("INPUT");
for(var i=0; i<inputObjs.length; i++)
{
 if(inputObjs[i].type == "RADIO")
 {
  for(var j=0; j<inputObjs[i].length; j++)
   inputObjs[i][j].disabled = true;
 }
}

I'm guessing this isn't the most effective way to achieve what you're after. If this doesn't help you, give us a little more detail.

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top