Search code examples
javascriptjquerymaterialize

MaterializeCSS Component Missing Something Critical


I've been struggling to get MaterializeCSS elements to work properly in my project. None of the JS functions work and I'm not sure why.

This is my JSFiddle that doesn't work.

As far as I can tell, it's an exact replica of this one that does. So what's missing?

$( document ).ready(function() {
      $('.modal').modal();
      $('#modal1').on('click', function() {
      });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/js/materialize.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/css/materialize.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<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>

Thanks


Solution

  • The problem is: materialize needs jquery so you need to import jquery first before materialize.js

    enter image description here

    $( document ).ready(function() {
          $('.modal').modal();
          $('#modal1').on('click', function() {
          });
        });
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/js/materialize.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/css/materialize.min.css" rel="stylesheet"/>
    <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>