Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add the Bootstrap modal class to the modal container.
Bootsrap
<div class="modal [1]" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <!-- Modal content here --> </div> </div> </div>
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'show' class initially instead of 'fade'.
Forgetting to add any class for modal animation.
✗ Incorrect
The Bootstrap modal requires the 'fade' class to enable the fade-in effect and proper modal behavior.
2fill in blank
mediumComplete the code to set the modal's initial focus to the close button when it opens.
Bootsrap
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" [1]></button>
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tabindex="-1"' which removes the element from tab order.
Setting 'disabled' which prevents interaction.
✗ Incorrect
Adding 'autofocus' ensures the close button receives focus when the modal opens, improving keyboard navigation.
3fill in blank
hardFix the error in the JavaScript code that traps focus inside the modal.
Bootsrap
const modal = document.getElementById('myModal'); modal.addEventListener('keydown', (event) => { if (event.key === 'Tab') { const focusableElements = modal.querySelectorAll('a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])'); const firstElement = focusableElements[0]; const lastElement = focusableElements[focusableElements.length - 1]; if (event.shiftKey) { if (document.activeElement === firstElement) { event.preventDefault(); [1].focus(); } } else { if (document.activeElement === lastElement) { event.preventDefault(); firstElement.focus(); } } } });
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'firstElement' instead of 'lastElement' for backward tabbing.
Trying to focus the modal container instead of a focusable element.
✗ Incorrect
When Shift+Tab is pressed on the first focusable element, focus should move to the last element to trap focus inside the modal.
4fill in blank
hardFill both blanks to create an object using reduce that maps each input's id to its value only if the input is focused.
Bootsrap
const inputs = Array.from(document.querySelectorAll('input')); const focusedInputs = inputs.reduce((acc, input) => { if (document.activeElement === input) { acc[[1]] = [2].value; } return acc; }, {});
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping keys and values.
Using input.value as key which is incorrect.
✗ Incorrect
The key should be the input's id ('input.id') and use 'input' so {{BLANK_2}}.value resolves to input.value, mapping ids to values for focused inputs.
5fill in blank
hardFill all three blanks to create a function that opens a modal, sets focus to the first focusable element, and traps focus inside.
Bootsrap
function openModal(modalId) {
const modal = document.getElementById([1]);
modal.classList.add([2]);
const focusableElements = modal.querySelectorAll('a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])');
const firstElement = focusableElements[0];
firstElement.[3]();
modal.addEventListener('keydown', (event) => {
if (event.key === 'Tab') {
const lastElement = focusableElements[focusableElements.length - 1];
if (event.shiftKey) {
if (document.activeElement === firstElement) {
event.preventDefault();
lastElement.focus();
}
} else {
if (document.activeElement === lastElement) {
event.preventDefault();
firstElement.focus();
}
}
}
});
}Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the modal id.
Adding wrong class like 'fade' instead of 'show'.
Calling 'blur()' instead of 'focus()' on the element.
✗ Incorrect
The modal is selected by id 'myModal', the 'show' class is added to display it, and the first focusable element is focused using the 'focus()' method.
