How to disable a switch using javascript? (Do not use JS infrastructure)

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, document.formName.disableme.disabled = true; But that did not work.

I can do this with Id. But I need it with one shot.

Please, help.

+6
source share
8 answers

I tried like, document.formName.disableme.disabled = true; But that did not work.

Because if you have more than one format control with the same name, you will return the HTML Form Controls Collection . So, let's move on to the collection:

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

No need to add an identifier.

+16
source
 document.getElementById("1").disabled=true; document.getElementById("2").disabled=true; document.getElementById("3").disabled=true; 
+5
source

First of all, your identifiers should be quoted in double quotes. eg:

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

and to disable the button using javascript use the following command:

 document.getElementById("1").disabled=true; 
+3
source

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 
+3
source

You cannot do it in one shot.

You have three separate elements, so you have to disable them one at a time. It is not possible to use simple Javascript to disable all three elements at once.

document.formName.disableme returns an array, so you cannot access its properties, but you can scroll through the elements and access the properties for each element:

 var radios = document.formName.disableme; for (var i = 0; i < radios.length; i++) { radios[i].disabled = true; } 

Demo: http://jsfiddle.net/Guffa/2uUKw/

+2
source

If you want to use only the name attribute to disable it, you can use

document.getElementsByName("disableme")[0].disabled = true;

+2
source
 function disable() { var rads = document.formname.disableme; for(var i=0; i<rads.length;i++ ) { document.formname.disableme[i].disabled = true; } } 
+1
source

This is the easiest way to do (even if you have the same id ) for the switch

Switch Definition:

 <input type="radio" name="paymentType" id="paymentType" value="CASH" /> CASH <input type="radio" name="paymentType" id="paymentType" value="CHEQUE" /> CHEQUE <input type="radio" name="paymentType" id="paymentType" value="DD" /> DD 

Disabling all 3 values:

  document.formName.paymentType[0].disabled = false; document.formName.paymentType[1].disabled = false; document.formName.paymentType[2].disabled = false; ... 
0
source

Source: https://habr.com/ru/post/1444263/


All Articles