I have an html page how can i specify tab index for bootstrap popup modal and main differently .
If popup is open table index should be available for popup modal only. But when i click on last element of popup modal tab transfer control to main page.
<a href="#">first element</a>
.
.
...
<a href="#" >last element in popup</a>
when i press tab on last a it should transfer focus to first element instead of transferring to main page content
I have an html page how can i specify tab index for bootstrap popup modal and main differently .
If popup is open table index should be available for popup modal only. But when i click on last element of popup modal tab transfer control to main page.
<a href="#">first element</a>
.
.
...
<a href="#" >last element in popup</a>
when i press tab on last a it should transfer focus to first element instead of transferring to main page content
Share Improve this question edited Nov 23, 2015 at 4:39 Muhammad Nasir asked Nov 18, 2015 at 8:05 Muhammad NasirMuhammad Nasir 2,2044 gold badges37 silver badges65 bronze badges 1- 1 Bootstrap modal will have restriction of tab index inside modal by default. Check out the example here: getbootstrap./javascript/#modals. You are using some other modal? – Ganesh Kumar Commented Nov 23, 2015 at 11:00
4 Answers
Reset to default 4 +50add id to select in dom
<a href="#" id="first">first element</a>
.
.
...
<a href="#" id="last" >last element in popup</a>
Check if event raised by last a is tab then set focus to first element
<script type="text/javascript">
document.getElementById('last').onkeydown = function(e){
if (e.keyCode == 9) {
document.getElementById('first').focus();
}
}
</script>
if you mean when you press tab from the last element and the modal closes, then try adding these inside the modal parameters.
( make sure your modal contains a close button, otherwise you will not be able to close the modal as the modal will not close on a input from the "esc" button or a click outside the modal body.)
data-backdrop="static" data-keyboard="false"
but if you want to close the modal by clicking out of the modal then use only
data-keyboard="false"
try something like below
$('#myModal').keydown(function(e){
if($('#last').is(":focus") && (e.which || e.keyCode) == 9){
e.preventDefault();
$('#first').focus();
}
});
You can do something like following:
private tabKey(event: KeyboardEvent) {
let parentModal = $(document).find('.modal');
//List of html elements which can be focused by tabbing.
let focusableElementsArrayString = 'a[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, [tabindex="0"]';
let focusableElementsInModal = parentModal.find(focusableElementsArrayString);
let numberOfElements = focusableElementsInModal.length;
let firstTabElement = focusableElementsInModal[0];
let lastTabElement = focusableElementsInModal[numberOfElements - 1];
// Check for Shift + Tab
if (event.keyCode === 9 && event.shiftKey) {
if (document.activeElement === firstTabElement) {
event.preventDefault();
lastTabElement.focus();
} // Check for Tab
} else if (event.keyCode === 9) {
if (document.activeElement === lastTabElement) {
event.preventDefault();
firstTabElement.focus();
}
}
}