I'm trying to create a new toast via javascript using the bootstrap 5 toasts
so basically I searched everywhere and nothing showed up, I do not want to toggle or trigger an existing toast, all I want is to create one and show it
there are no javascript methods to create one, in bootstrap documentation, there is only create an Instance to initialize an existing one, is there a way? https://getbootstrap.com/docs/5.2/components/toasts/
I appreciate all answers
The easiest way to completely create the toast with Javascript and not use an existing HTML element would be to use document.createElement() to create the HTML divs and then add the needed classes, as described here to it.
If you want to get the following:
<div class="toast show">
<div class="toast-header">
Toast Header
</div>
<div class="toast-body">
Some text inside the toast body
</div>
</div>
You would do this:
const toastContainer = document.createElement('div');
toastContainer.classList.add('toast', 'show');
const toastHeader = document.createElement('div');
toastHeader.classList.add('toast-header');
toastHeader.innerText = 'Toast Header';
const toastBody = document.createElement('div');
toastBody.classList.add('toast-body');
toastBody.innerText = 'Some text inside the toast body';
toastContainer.appendChild(toastHeader);
toastContainer.appendChild(toastBody);
document.body.appendChild(toastContainer);
Here I added the created divs to the body, but of course they can also be added to other divs.
Wanted to share my solution in case someone needs something similar like this.
The element should appear on the bottom left of the page. It is also possible to stack multiple toasts over each other, therefore we need a wrapping element that wraps them correctly
function createToastElement(type, content) {
let toastContainer = document.createElement('div');
toastContainer.classList.add('toast-container');
let toast = document.createElement('div');
toast.classList.add('toast');
let toastBody = document.createElement('div');
toastBody.classList.add('toast-body', type);
toastBody.innerText = content;
toast.appendChild(toastBody);
toastContainer.appendChild(toast);
if ($('.wrapping-element').length <= 0) {
let wrappingElement = document.createElement('div');
wrappingElement.classList.add('wrapping-element');
wrappingElement.appendChild(toastContainer);
document.body.appendChild(wrappingElement);
}
else {
$('.wrapping-element').append(toastContainer);
}
}
Depending on the type the toast should be visible for more or less amounts of seconds. After the toast is hidden it gets removed from the dom
function toast(type, content) {
createToastElement(type, content);
let toastElement = $('.toast');
toastElement.toast({
delay: type === 'danger' || type === 'warning' ? 20000 : 3000,
});
toastElement.toast('show');
toastElement.on('hidden.bs.toast', function () {
toastElement.remove();
});
}
I have a scss with the following classes.
.wrapping-element {
position: fixed;
bottom: 0;
left: 0;
right: 0;
}
.toast-container{
margin: 15px;
}
.toast-body {
min-width: 300px;
color: white;
background-color: black;
&.success {
background-color: green;
}
&.info {
background-color: blue;
}
&.warning {
background-color: yellow;
}
&.danger {
background-color: red;
}
}
toast('danger', 'Bitte Zeitspanne auswählen');

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With