I’m using Bootstrap 5.3 right out of the box to create a toast message. The console says that within this code:
const toastTrigger = document.getElementById('buynow')
const toastLiveExample = document.getElementById('liveToast')
if (true) {
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastLiveExample)
toastTrigger.addEventListener('click', () => {
toastBootstrap.show()
});
};
that bootstrap is not defined. I’m stumped. I don’t know of anything I can tweak to fix this error.
<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe"
crossorigin="anonymous"></script>
<script src="app.js"></script>
<script src="https://kit.fontawesome.com/b20ad3118a.js" crossorigin="anonymous"></script>
</body>
</html>
2
Answers
Did you import ‘bootstrap’ into your component?
9 times out of 10 when there is an undefined error it’s because something wasn’t properly imported (or required) in a component or script.
This code has no issues if you import the library correctly.
I suggest checking if the library success imported in
Inspect Element > Network > JS
and ensuring all status is200
. Make sure your internet/proxy doesn’t block the library.I have checked your codes. There are a few problem can be solved:
app.js
to the bottom ofbody
because the browser loads components in ascending order from top to down, so.getElementById
will benull
if<script>
at the top position of the targeted id..stretched-link
classes fromBuy Now
button because this class blocking the click event of this button.width: 30%
at the logo of the toast to prevent a bigger size of it.Continuation:
To fix this, you need to put
div.data-bs-target
on<main>
tag and close that tag on<footer>
tag.Simple, you can import popper.min.js library before
bootstrap.min.js
That’s it.