I'd suggest:
var inputs = document.getElementsByName('layouts');
for (var i = 0, len = inputs.length; i<len; i++){
inputs[i].disabled = true;
}
Simple demo.
This will select the relevant elements with the name
of layouts
, and then, in the for {...}
loop, iterate over those elements and set the disabled
property.
Using a simple function approach:
function disableByName(elName){
var els = document.getElementsByName(elName);
if (els !== null){
for (var i = 0, len = els.length; i<len; i++){
els[i].disabled = true;
}
}
}
var button = document.getElementById('radioDisable');
button.addEventListener('click',function(e){
e.preventDefault();
disableByName('layouts');
}, false);
Simple demo.
Or, if you'd prefer, you can extend the Object prototype to allow you to directly disable those elements returned by the document.getElementsByName()
selector:
Object.prototype.disable = function(){
var that = this;
for (var i = 0, len = that.length; i<len; i++){
that[i].disabled = true;
}
return that;
};
document.getElementsByName('layouts').disable();
Simple demo.
id
? – Panettone