Joe Hidakatsu
Joe Hidakatsu

Reputation: 464

How to dismiss stacking toasts in bootstrap?

I'm trying to get stacking bootstrap toasts to work, but the close buttons are not working. I can close the most recent toast but no others.

I've tried adding an event listener to the close button directly, but it seems the listener does not fire.

document.getElementById('activate').addEventListener('click', () => {
  createToast('hello', 'a', 'b');
});

function createToast(title, smallText, text) {
  let id = new Date().getTime();
  let html = `<div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-autohide="false" id=${id}>
                <div class="toast-header">
                    <svg class="bd-placeholder-img rounded mr-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg"
                    preserveAspectRatio="xMidYMid slice" focusable="false" role="img">
                    <rect fill="#007aff" width="100%" height="100%" /></svg>
                    <strong class="mr-auto">${title}</strong>
                    <small>${smallText}</small>
                    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="toast-body">
                    ${text}
                </div>
            </div>`;

  document.getElementById('toast-container').innerHTML += html;
  $(`#${id}`).toast('show');
}
<html>

<head>
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>

<body>
  <div id="toast-container"></div>
  <button id="activate">Go</button>
</body>

</html>

This code can be copy and pasted and ran.

Upvotes: 1

Views: 6097

Answers (1)

madalinivascu
madalinivascu

Reputation: 32354

Use toast('hide')

document.getElementById('activate').addEventListener('click', () => {
            createToast('hello', 'a', 'b');
        });

        function createToast(title, smallText, text) {
            let id = new Date().getTime();
            let html = `<div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-autohide="false" id=${id}>
                <div class="toast-header">
                    <svg class="bd-placeholder-img rounded mr-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg"
                    preserveAspectRatio="xMidYMid slice" focusable="false" role="img">
                    <rect fill="#007aff" width="100%" height="100%" /></svg>
                    <strong class="mr-auto">${title}</strong>
                    <small>${smallText}</small>
                    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="toast-body">
                    ${text}
                </div>
            </div>`;

            document.getElementById('toast-container').innerHTML += html;
            $(`#${id}`).toast('show');
        }
        $('body').on('click','.close',function(){
          $(this).closest('.toast').toast('hide')
        })
        
<html>
    <head>
            <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
            <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    </head>

    <body>
        <div id = "toast-container"></div>
        <button id = "activate">Go</button>
    </body>

</html>

Upvotes: 3

Related Questions