Home > Back-end >  JS on checkbox Change enable or disable a radio button
JS on checkbox Change enable or disable a radio button

Time:06-25

I have a check box that I want to enable these radio buttons. what I have does not work. how can I JS that?

im new. :(

var sipch = document.querySelector("input[name=sip]");
sipch.addEventListener( 'change', function() {
    if(sipch.checked) {
        document.getElementById("protocol").disabled=false;
    } else {
        document.getElementById("protocol").disabled=true;
    }
});
<input type="checkbox" name="sip" onsubmit="goPFive(event)" method="get">do this?
<input type="radio"  name="protocol" value="udp" disabled checked/>UDP
<input type="radio"  name="protocol" value="tcp" disabled />TCP

CodePudding user response:

My solution:

const sipch = document.querySelector("input[name=sip]");
sipch.addEventListener('change', function(e) {
  let eles = document.querySelectorAll(".testD")
  if (e.target.checked) {
    eles.forEach(ele => ele.disabled = false);
  } else {
    eles.forEach(ele => ele.disabled = true);
  }
});

CodePudding user response:

group your radio input with fieldset then set disabled attribute at fieldset element

<fieldset id="radioWrapper" disabled>
  <input type="radio" name="protocol"  value="udp"/>UDP
  <input type="radio" name="protocol"  value="tcp" />TCP
</fieldset>
  • Related