Search code examples
javascriptsweetalert2

(Sweetalert2 Toast) How to disable ENTER/SPACE, ESC & click outside the box?


Tried the following and they doesn't seem compatible.

  1. allowOutsideClick
  2. allowEscapeKey
  3. allowEnterKey

Is there a workaround to be able to prevent Enter, ESC & click outside the Toast popup box? So the users must have to wait for the popup to close to do anything.


Solution

  • try to catch event and disable it with event.preventDefault ()

    document.write("press a key")
    document.addEventListener("keypress",(e)=>{
            if(e.keyCode==27 || e.keyCode==13){
              e.preventDefault()
              alert(" you can't press esc or enter")
            }
            else document.write("key press:"+e.keyCode)
            })
    document.addEventListener("click",(e)=>{
    if(e.path[0].className!=="myAlert"){
      e.preventDefault()
      alert("you can't press out")
      }
    })
    .myAlert{
         width: 100px;
         height: 100px;
         background: red;
         color: white;
         text-alin
    }
    <div class="myAlert">
      Click me
    </div>

    visit the documentation