Home > database >  Initializing `Materialize` select on Google App Script
Initializing `Materialize` select on Google App Script

Time:01-03

I am trying to write a basic form that includes a select element with the Materialize framework.

However, the select element does not appear to be getting initialized.

Here is my code (based on the example here):

<html>

  <head>
    <!--Import Google Icon Font-->
    <link href="https://fonts.googleapis.com/icon?family=Material Icons" rel="stylesheet">
    <!--Import materialize.css-->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
    <!--Let browser know website is optimized for mobile-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  </head>

  <body>
    
    <form  id="form" novalidate="novalidate" style="max-width: 400px;margin: 40px auto;">

    <!-- Account  -->
    <div >
      <select>
        <option value="" disabled selected>Choose your option</option>
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
        <option value="3">Option 3</option>
      </select>
      <label>Materialize Select</label>
    </div>

    </form>
  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js">
    </script>


    <!--Initialize `select` elements-->
    <script>
      document.addEventListener('DOMContentLoaded', function() {
      var elems = document.querySelectorAll('select');
      var instances = M.FormSelect.init(elems, options);
    });
    </script>

  </body>
</html>

CodePudding user response:

The problem was trivial: options was undefined. Removing it, as in the following line fixes things:

var instances = M.FormSelect.init(elems);
  • Related