I am implementing a Vue.js 2 feature for displaying Bootstrap modals. Here is an example.
<div class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" autofocus class="btn btn-primary">Always focus</button>
</div>
</div>
</div>
</div>
The goal is to allow users to press the Enter
key to click on the Always focus
button when the modal appears, without using the mouse. Additionally, pressing the left
or right
arrow keys should switch focus between the two buttons.
While the use of the autofocus
attribute works initially, it loses focus on the Always focus
button after the modal pops up for a second time.
How can I ensure that the Always focus
button always retains focus whenever the modal is displayed? Alternatively, are there CSS, JavaScript, or Vue.js solutions to achieve this behavior?
It's worth noting that I am specifically working with Vue.js 2 and would appreciate any solutions within that framework, excluding jQuery.