I have a select
element. The selected value determines whether a text input
box shows up afterwards (using ng-show
). For accessibility purposes, I want the user to be able to tab seamlessly through the inputs; as soon as they tab away from the select
, I want the new input box focused.
<div ng-controller="MyCtrl">
<label>Start here, then tab through the inputs:</label>
<div>
<input type="text" id="firstInput" placeholder="(my first input)" />
</div>
<label>Do you need another input box before moving on?</label>
<div>
<select ng-model="needAnotherInput">
<option value="false">No, I do not</option>
<option value="true">Yes, I do</option>
</select>
</div>
<div>
<input ng-show="needAnotherInput" type="text" />
</div>
<label>Here's the next thing to fill out:</label>
<div>
<input type="text" />
</div>
</div>
JSFiddle: http://jsfiddle.net/127k2urr/9/
You'll see that in Chrome and IE, this works fine; if you tab to the dropdown menu and arrow through the options (don't open the dropdown), the model updates with each new selection. But in Firefox, the model doesn't update until you tab away from the dropdown, so the new input box shows up too late; focus goes to the next one.
According to this discussion on the AngularJS GitHub, this might be related to when the browser fires the onchange event, and Firefox may be doing it properly according to spec. Regardless, I want to deal with these cases gracefully and in a generalizable way. Any solution also must account for SHIFT+TAB, which takes you backwards in the DOM through the focusable elements. In another StackOverflow thread, someone gives an example of a directive that will set focus to an element as soon as it shows up. I'll try putting something like that on the sometimes-visible next input and report back.