0

Why is the checked radio input not triggered by the onchange event after checking a different radio input with the same name attribute value.

Have a look at this jsfiddle. Try to trigger at least two onchange events, as you can see the listener will only trigger if the radio input is checked. When one of the radio inputs is checked and you check the other one, then the one previously being checked won't trigger the onchange event.

For checkboxes this event is triggered when unchecked, why not for radio inputs?

luukvhoudt
  • 1,726
  • 19
  • 33
  • I believe you are wanting the onchange event to trigger when the value is changed. But it doesn't work that way. These events are invoked by user interactions. Reference: https://stackoverflow.com/questions/590289/javascript-event-that-fires-without-user-interaction#590320 – Gordon Kushner Feb 19 '18 at 21:13
  • @Gordon Kushner with the value you mean the value what will be submitted. If so I perhaps slightly have to modify my question to: how to detect checked state changes in radio inputs. – luukvhoudt Feb 21 '18 at 06:59
  • perhaps I'm unclear on what you're looking for. What I'm seeing in your checkbox Fiddle is that when the USER unchecks a checkbox, an event is fired. But in the radio Fiddle, the user checks the other one and no event is raised on the one losing the selection. But that's because the user is not unselecting, the browser behavior is and thus, no event. – Gordon Kushner Feb 21 '18 at 14:18

1 Answers1

0

I believe the radio buttons should be targeted this way: $('input[name="test"]')

$('input[name="test"]').change(function(){
 $('#output').append('<p>'+this.value +' changed</p>');
});
#output {
  margin-top: 20px;
  padding: 10px;
  background-color:slategrey;
}
p {
  margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label><input type="radio" name="test" value="foo" />Foo</label>
<label><input type="radio" name="test" value="bar" />Bar</label>
<div id="output"></div>
spierce
  • 101
  • 10
  • That depends on the situation. However your answer is not related to my question. Please read it again and feel free to comment if you got any question. – luukvhoudt Feb 19 '18 at 22:02