Search code examples
htmlmaterialize

How to disable modal closing when clicking outside? [Materialize]


I have a materialize modal and I would like to know how to disable the closing of it when you click away. Follow my code:

<div id="hscal" class="modal modal-fixed-footer">
        <h1 class="h4">Histórico de Calibração</h1>
        <div class="modal-content">
          <table id="bodymodal" class="highlight centered"></table>
        </div>
        <div class="modal-footer">
          <button
            class="btn waves-effect waves-light col-sm-1 col-md-2 col-lg-2 close-modal"
            type="reset"
            id="fx"
            name="action"
            href="#"
            v-on:click="exitModal"
          >Fechar
            <i id="iconbtn" class="material-icons right">clear</i>
          </button>
        </div>
      </div>

Solution

  • You can set this and many other modifications during initialisation:

    // with jQuery
    
    $('.modal').modal({
       dismissible: false
    });
    
    // Vanilla JS
    
    document.addEventListener('DOMContentLoaded', function() {
        var elems = document.querySelectorAll('.modal');
        var instances = M.Modal.init(elems, {
           dismissible: false
        });
    });