Complete the code to create a basic toast container with Bootstrap.
<div aria-live="polite" aria-atomic="true" class="position-relative"> <div class="toast-container [1] p-3" style="position: absolute; top: 0; right: 0;"> <!-- Toasts will appear here --> </div> </div>
The class top-0 end-0 places the toast container at the top right corner, which is a common position for toast notifications.
Complete the code to add a toast with a header and body using Bootstrap classes.
<div class="toast show" role="alert" aria-live="assertive" aria-atomic="true"> <div class="toast-header"> <strong class="me-auto">Bootstrap</strong> <small class="text-muted">just now</small> <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="[1]"></button> </div> <div class="toast-body"> Hello, this is a toast message. </div> </div>
The aria-label for the close button should be close to clearly describe its function to screen readers.
Fix the error in the JavaScript code to properly initialize and show the toast.
const toastEl = document.getElementById('myToast'); const toast = new bootstrap.Toast([1]); toast.show();
The bootstrap.Toast constructor expects the DOM element, which is stored in the variable toastEl.
Fill both blanks to create a toast that autohides after 3 seconds.
const toastEl = document.getElementById('autoToast'); const toast = new bootstrap.Toast(toastEl, { delay: [1], [2]: true }); toast.show();
autoHide which is incorrect.The delay option sets the time in milliseconds (3000 ms = 3 seconds). The correct option to enable auto hiding is autohide (all lowercase).
Fill all three blanks to create a toast with a header, body, and a button that triggers the toast to show on click.
<button type="button" class="btn btn-primary" id="showToastBtn">Show Toast</button> <div class="toast" id="myToast" role="alert" aria-live="assertive" aria-atomic="true"> <div class="toast-header"> <strong class="me-auto">[1]</strong> <small class="text-muted">[2]</small> <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button> </div> <div class="toast-body"> [3] </div> </div> <script> const toastEl = document.getElementById('myToast'); const toast = new bootstrap.Toast(toastEl); document.getElementById('showToastBtn').addEventListener('click', () => { toast.show(); }); </script>
The toast header title is 'Notification', the small text shows 'just now' as the time, and the body contains the message 'This is a toast message.'.