Materialize: does not work initialization modal

Created on 3 Apr 2018  路  14Comments  路  Source: Dogfalo/materialize

<div id="data_modal" class="modal"> 
    <div class="modal-content"> 
        <h4>Modal Header</h4> 
        <div class="row">
            <div class="col l12">
                A bunch of text 
            </div>
        </div>
    </div> 
    <div class="modal-footer"> 
        <a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat" id="save_frame">小芯褏褉邪薪懈褌褜</a> 
    </div> 
</div>
md5-0e61fae6f34b7df3c7504aa5f219cd91


===error

Uncaught TypeError: Cannot read property 'open' of undefined
at _show_data_modal (lm=doc_edit&did=1:183)
at eval (eval at success (scripts.js?v1522782874:20), :1:1)
at Object.success (scripts.js?v1522782874:20)
at i (jquery-3.2.1.min.js:2)
at Object.fireWith [as resolveWith] (jquery-3.2.1.min.js:2)
at A (jquery-3.2.1.min.js:4)
at XMLHttpRequest. (jquery-3.2.1.min.js:4)

Most helpful comment

All 14 comments

You have to call getInstance on the DOM Element not the jQuery wrapper on the element.

I decided it was easier
just added

$(document).ready(function(){
    $('#data_modal').modal();
});

we have same issue
Modal


i have founded error "Uncaught TypeError: Cannot read property 'open' of undefined"

@socareta You didn't initialize the modal

var elem= document.querySelector('.modal');
var instance = M.Modal.init(elem, '');
M.Modal.getInstance(instance).open();
finally modal working with same error

thank you so much, we have been tried using single html and working. but we use vue js. still have same error.

great..now working...thanks

@socareta How did you solve it? The link provided by Dogfalo didn't really help me understand. Im trying to create an instance so I can call 'instance.close();' when button pressed in modal.

@jullemeister i am try use var elem= document.querySelector('.modal'); var instance = M.Modal.init(elem); onclick="instance .open()"

@Dogfalo
I tried this
console.log(M.Modal.getInstance(document.getElementById('loginModal')));
but I got undefined

@Dogfalo Nevermind I didn't put it in the DOMContentLoaded event listener

You can use this

Get element by ID
var elems = document.getElementById('idDivModal');

The next method is OPTIONAL

With this method you can reconfigure your DIV MODAL and you can add more options

var instances = M.Modal.init(elems, { 
   opacity: 0.5
});

New instance
var instance = M.Modal.getInstance(elems);
instance.open(); // instance with methods

Was this page helpful?
0 / 5 - 0 ratings

Related issues

heshamelmasry77 picture heshamelmasry77  路  3Comments

onigetoc picture onigetoc  路  3Comments

serkandurusoy picture serkandurusoy  路  3Comments

Robouste picture Robouste  路  3Comments

bradley-varol picture bradley-varol  路  3Comments