I have a set of radio buttons on a form, and using jQuery I want to detect when a user has changed the value of these inputs. I can do something like $('input.myinputs').on('change', ... );
, except that if a user is using a keyboard to navigate, that fires every time they move from one button to the next. In other words it fires multiple times in the process of the user making a selection. (Clicking with a mouse works fine.)
I want to make sure the function only fires when the user leaves the radio group (the set of same-named radio buttons). I should be able to tab into the radio group, navigate up or down via arrow keys, and then when I tab out of the radio group, THEN the function fires once.
Is there a way to do this? Everything I've tried fires every time I hit an arrow key if the focus is within the group.
Example:
You've got a small form:
- a text input
- a radio group containing four options labelled A, B, C, D; and
- a second text input
Click on the first text input. Now put down your mouse and hit the Tab key. Use the arrows to select the third option. Now hit Tab to move to the second text input. That triggers the "change/blur" event on the radio group three times - when the focus moved into the radio group, which selected A; then when you arrowed to B, then again when you arrowed to C.
I want an event triggered one time when I move the focus from the radio group to the second text input (that is, out of the radio group), but only if I changed what was selected in the radio group. I need "on change" for the group instead of for each individual option.
It should still work for mouse or mobile (touch) users of course; I'm accommodating keyboard navigators, while acknowledging that most users use a mouse or their finger.
Example HTML:
<input type="text">
<div class="inputApprove">
<label><input type="radio" name="R1" value="A" checked="checked"> A</label>
<label><input type="radio" name="R1" value="B"> B</label>
<label><input type="radio" name="R1" value="C"> C</label>
<label><input type="radio" name="R1" value="D"> D</label>
</div>
<input type="text">