I create one element in my radioGroup like this:
var selectorLay1 = document.createElement('input');
var selectorLay1Atributes = {
'type': 'radio',
'class': "selectorLay1",
'id': "radioLay1",
'name': "layouts",
'onchange': "mv.createLayout(1,1)"};
I have different elements like this. But they all have the same name: 'layouts'
.
How to find all of this elements and disable them dynamically.
Try this:
var radios = document.getElementsByName('layouts');
for (var i = 0, r=radios, l=r.length; i < l; i++){
r[i].disabled = true;
}
Read https://developer.mozilla.org/en-US/docs/DOM/document.getElementsByName for getElementsByName
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With