I created table with a double loop.
For every cell i put 3 radios and a button that should uncheck the radios.
I can’t do that because the functions inside the loop can’t identify the correct radio (that has id based on 2 vars: ‘NP’+medici[cas]+colonne[col]).
If I put the function outside the loop:
- if I put the function after the loop I get “function not declared” error
- if I put the function before the loop I get “Cannot set properties of null (setting ‘checked’)”
This is the function
for(let cas=0; cas<6; cas++){
[...]
for (var col=1; col<6; col++){
[...]
var radioboxNM = document.createElement('input');
radioboxNM.type = 'radio';
radioboxNM.id = 'NM'+medici[cas]+colonne[col];
radioboxNM.value = 'NM'+medici[cas]+colonne[col];
radioboxNM.name = 'radio'+medici[cas]+colonne[col];
var label = document.createElement('label')
label.htmlFor = 'NM';
var description = document.createTextNode('NM');
label.appendChild(description);
const newline = document.createElement('br');
tbodyTdAss.appendChild(radioboxNM);
tbodyTdAss.appendChild(label);
//crea radio NP
var radioboxNP = document.createElement('input');
radioboxNP.type = 'radio';
radioboxNP.id = 'NP'+medici[cas]+colonne[col];
radioboxNP.value = 'NP'+medici[cas]+colonne[col];
radioboxNP.name = 'radio'+medici[cas]+colonne[col];
var label = document.createElement('label');
label.htmlFor = 'NP';
var description = document.createTextNode('NP');
label.appendChild(description);
tbodyTdAss.appendChild(radioboxNP);
tbodyTdAss.appendChild(label);
tbodyTdAss.appendChild(newline);
//Crea radio assente
var radioboxAss = document.createElement('input');
radioboxAss.type = 'radio';
radioboxAss.id = 'Ass'+medici[cas]+colonne[col];
radioboxAss.value = 'Ass'+medici[cas]+colonne[col];
radioboxAss.name = 'radio'+medici[cas]+colonne[col];
var label = document.createElement('label');
label.htmlFor = 'Ass';
var description = document.createTextNode('Ass');
label.appendChild(description);
tbodyTdAss.appendChild(radioboxAss);
tbodyTdAss.appendChild(label);
// crea bottone reset
buttonX[cas][col] = document.createElement('button');
buttonX[cas][col].innerText = 'Canc';
buttonX[cas][col].id = 'buttonX'+medici[cas]+colonne[col];
buttonX[cas][col].setAttribute("onClick", cancellaRadio(cas,col));
[...]
This is the function:
cancellaRadio = function (cas,col) {
document.getElementById('NM'+medici[cas]+colonne[col]).checked = false;
document.getElementById('NP'+medici[cas]+colonne[col]).checked = false;
document.getElementById('Ass'+medici[cas]+colonne[col]).checked = false;
};
Any ideas?