Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Materialize modal not working

I wrote a simple code for materialize modal.
HTML code:

<a class="waves-effect waves-light btn view" data-target="modal1">View Scores</a>
<!-- Modal Structure -->
<div id="modal1" class="modal">
  <div class="modal-content">
    <h4>Modal Header</h4>
    <p>A bunch of text</p>
  </div>
  <div class="modal-footer">
    <a href="#!" class=" modal-action modal-close waves-effect waves-green btn-flat">Agree</a>
  </div>
</div>  

JS code:

$(document).ready(function() {
  // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered
  /*$('.view').click(function (){
    $('#modal1').modal('open'); 
    alert('edskjcxnm');
  });*/
  /*$('.view').leanModal();*/
  $('#modal1').modal('open');
});  

JSFiddle link: https://jsfiddle.net/7f6hmgcf/
Why isn't it working?

like image 269
Ajay Kulkarni Avatar asked Jan 31 '17 19:01

Ajay Kulkarni


People also ask

How to use modal in Materialize?

In order for the modal to work, add the modal Id to the link of the trigger and include the reference materialize. min. js along with jQuery on the web page. To add a “close” button, add the class modal-close to your button.

What is modal footer?

A modal footer is used to customize the default footer of a modal dialog.


1 Answers

Initialize all modals first. $('.modal').modal();

Complete code will look like this

(function ($) {
    $(function () {

        //initialize all modals           
        $('.modal').modal();

        //now you can open modal from code
        $('#modal1').modal('open');

        //or by click on trigger
        $('.trigger-modal').modal();

    }); // end of document ready
})(jQuery); // end of jQuery name space
like image 117
Alexey Avatar answered Sep 28 '22 05:09

Alexey