Put values on each radio button. It will be useful if you submit to the server and useful to use in the script to differentiate one element from the other (no need to use id
that is unnecessary for this):
<input type="radio" name ="social" value="sim"> Sim
<input type="radio" name="social" value="nao" checked> Nao
In the first place value="sim"
and in the second value="nao"
.
Since checkboxes are already loaded disabled, it is interesting
also add attribute checked
in the "Nao" option, so that it is already marked.
It is good also to put a class in each checkbox
so you can create a group and not confuse it with other checkboxes you may have on the page:
<input class="social" type="checkbox" name="facebook" disabled> facebook<br>
<input class="social" type="checkbox" name="linkedin" disabled> linkedin<br>
<input class="social" type="checkbox" name="instagram" disabled> instagram<br>
With this, just create an event onchange
for each radio button and change the checkbox according to the value of the radio changed:
const radios = document.querySelectorAll("[name='social']");
const social = document.querySelectorAll(".social");
for(let i of radios){
i.onchange = function(){
for(let x of social){
x.disabled = i.value == "sim" ? false : true;
}
}
}
<label>Possui rede social: </label>
<input type="radio" name ="social" value="sim"> Sim
<input type="radio" name="social" value="nao" checked> Nao <br><br>
<label>Quais</label><br><br>
<input class="social" type="checkbox" name="facebook" disabled> facebook<br>
<input class="social" type="checkbox" name="linkedin" disabled> linkedin<br>
<input class="social" type="checkbox" name="instagram" disabled> instagram<br>
you have already seen the related questions here on the site, for example: https://answall.com/questions/292060/selectr-todos-checkbox-ao-clicar-em-um-checkbox ?
– Ricardo Pontual
this way does not work, need only enable, not give automated checked.
– Decristony
In your "enable" function, Voce can remove the "disabled" attribute from input[checkbox]. Ex:
element.removeAttribute("disabled");
. More info here MDN - Removeattribute– Douglas Garrido