-1

<input type="radio" name="gender" value="male">
<input type="radio" name="gender" value="female">

If a select 1 radio button other should disable automatically before i click submit.How can i achieve this using javascript?

CodeMonkey
  • 2,828
  • 1
  • 23
  • 32
  • Welcome to Stack Overflow. You should provide some code of what you have tried so far to solve the problem. Please keep in mind that Stack Overflow is not a code writing service. – gus27 Mar 03 '17 at 11:34
  • Have you tried something ? Show us your effort. – Code Lღver Mar 03 '17 at 11:34
  • Possible duplicate of [Javascript onclick event handling with pure JavaScript](http://stackoverflow.com/questions/17633152/javascript-onclick-event-handling-with-pure-javascript) and [Disabling and enabling a html input button](http://stackoverflow.com/questions/13831601/disabling-and-enabling-a-html-input-button) – Nope Mar 03 '17 at 11:36
  • 1
    Why will you disable it? With a radio button you can only select one of the options... – gus27 Mar 03 '17 at 11:37

1 Answers1

0

If you check any of the radio inputs, the another one will be disabled automatically.

var elems = document.getElementsByTagName('input');

Array.from(elems).forEach(v => v.addEventListener('change', function(){
  Array.from(elems).find(c => !c.checked).disabled = true;
  console.log(`You selected: ${this.value}`);
}));
<input type="radio" name="gender" value="male">
<input type="radio" name="gender" value="female">
kind user
  • 40,029
  • 7
  • 67
  • 77