Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the radio button using javascript ? (Not using any JS framework)

For example I have html elements like

<input type="radio" name="disableme" id=1> Animal
<input type="radio" name="disableme" id=2> Mammal
<input type="radio" name="disableme" id=3> Human

I tried like , document.formName.disableme.disabled = true; But it didn't worked..

I can do it using Id. But I need it in one shot.

Please help.

like image 242
Sahal Avatar asked Nov 06 '12 06:11

Sahal


2 Answers

Try this code

var radio=document.getElementsByName("disableme");
   var len=radio.length;
   for(var i=0;i<len;i++)
   {
       radio[i].disabled=true;
   }


<input type="radio" name="disableme" id=1> Animal
<input type="radio" name="disableme"  id=2> Mammal
<input type="radio" name="disableme"  disabled="disabled" id=3> Human
like image 97
Hkachhia Avatar answered Sep 30 '22 16:09

Hkachhia


I tried like , document.formName.disableme.disabled = true; But it didn't worked..

Because if you have more than one form control with the same name, you will get back an HTML Form Controls Collection. So loop over the collection:

var radios = document.formName.disableme;

for (var i=0, iLen=radios.length; i<iLen; i++) {
  radios[i].disabled = true;
} 

There is no need to add an ID.

like image 23
RobG Avatar answered Sep 30 '22 15:09

RobG