A modal is a popup box that appears on top of the page to show important information or ask for user action without leaving the current page.
Modal JavaScript API in Bootsrap
var myModal = new bootstrap.Modal(document.getElementById('myModalId'), options);
myModal.show();
myModal.hide();document.getElementById('myModalId') selects the modal element in your HTML.
options is optional and can control behavior like keyboard closing or backdrop.
var myModal = new bootstrap.Modal(document.getElementById('exampleModal'));
myModal.show();var myModal = new bootstrap.Modal(document.getElementById('exampleModal'), {keyboard: false});
myModal.show();var myModal = new bootstrap.Modal(document.getElementById('exampleModal'));
myModal.hide();This example shows a button that opens a modal popup when clicked. The modal has a title, some text, and a close button. It uses Bootstrap 5's JavaScript API to control the modal.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Modal Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <button id="openModalBtn" class="btn btn-primary">Open Modal</button> <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="modalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="modalLabel">Hello Modal</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> This is a simple modal using Bootstrap's JavaScript API. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> </div> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <script> const modalElement = document.getElementById('exampleModal'); const myModal = new bootstrap.Modal(modalElement); const openBtn = document.getElementById('openModalBtn'); openBtn.addEventListener('click', () => { myModal.show(); }); </script> </body> </html>
Always include Bootstrap's CSS and JavaScript files for the modal to work.
Use data-bs-dismiss="modal" on buttons to close the modal automatically.
Modals trap keyboard focus for accessibility, so users can navigate inside the modal using the keyboard.
Modals show content on top of the page without leaving it.
Use Bootstrap's JavaScript API to open and close modals with code.
Modals improve user experience by focusing attention and keeping context.
Practice
Solution
Step 1: Understand the purpose of the Modal JavaScript API
The API is designed to control modal visibility using JavaScript, not styling or content changes.Step 2: Identify the main feature
It allows opening and closing modals programmatically, which means without relying on user clicks.Final Answer:
Open and close modals programmatically without user clicks -> Option CQuick Check:
Modal API controls visibility [OK]
- Thinking it changes modal styles
- Assuming it resizes modals automatically
- Believing it adds content inside modals
Solution
Step 1: Recall Bootstrap modal instance creation syntax
Bootstrap requires using the constructor with the element: new bootstrap.Modal(element).Step 2: Match the correct syntax
const modal = new bootstrap.Modal(document.querySelector('#myModal')); uses new bootstrap.Modal with a DOM element selected by querySelector, which is correct.Final Answer:
const modal = new bootstrap.Modal(document.querySelector('#myModal')); -> Option AQuick Check:
Use new bootstrap.Modal(element) [OK]
- Using non-existent bootstrap.createModal method
- Omitting 'new' keyword
- Passing selector string instead of element
modal.show() is called?const modal = new bootstrap.Modal(document.getElementById('exampleModal'));
modal.show();Solution
Step 1: Understand what modal.show() does
The show() method displays the modal on the screen by changing its visibility and adding necessary classes.Step 2: Confirm modal initialization
The modal is correctly initialized with new bootstrap.Modal and a valid element, so show() works as expected.Final Answer:
The modal with id 'exampleModal' is displayed on the screen -> Option DQuick Check:
modal.show() displays modal [OK]
- Confusing show() with hide()
- Assuming show() is not a method
- Not initializing modal before calling show()
const modal = bootstrap.Modal(document.getElementById('myModal'));
modal.show();Solution
Step 1: Check modal instance creation syntax
Bootstrap modal instances require the 'new' keyword: new bootstrap.Modal(element).Step 2: Identify the error in the code
The code misses 'new', so modal is undefined or not a proper instance, causing show() to fail.Final Answer:
Missing 'new' keyword when creating the modal instance -> Option BQuick Check:
Always use 'new' to create modal instances [OK]
- Using open() instead of show()
- Passing selector string instead of element
- Calling show() on undefined variable
Solution
Step 1: Verify modal instance creation
const modal = new bootstrap.Modal(document.getElementById('autoCloseModal')); document.getElementById('openBtn').addEventListener('click', () => { modal.show(); setTimeout(() => modal.hide(), 3000); }); correctly uses 'new bootstrap.Modal' with a DOM element from getElementById, which is valid.Step 2: Check event listener and timing
The click event triggers modal.show(), then setTimeout calls modal.hide() after 3000ms (3 seconds), which matches the requirement.Final Answer:
const modal = new bootstrap.Modal(document.getElementById('autoCloseModal')); document.getElementById('openBtn').addEventListener('click', () => { modal.show(); setTimeout(() => modal.hide(), 3000); }); -> Option AQuick Check:
Use show() and hide() with setTimeout for auto-close [OK]
- Omitting 'new' keyword
- Using invalid methods like open() or close()
- Passing selector string instead of element
Ready to prove it?
Pass the test and this pattern moves unknown
