Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
How can i add more options to a select element in html?

i tried to make a select element in html using materialize css library, and when i try to add dynamic options to the select (for example, a list which each element in the list is an option of the select) and i can't find out why its only adding the first element

here is my code:

<html>
       <head>
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <script src = "https://code.jquery.com/jquery-1.12.4.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
          <script>
             jQuery(document).ready(function($){
             add_options_to_select();
             
             function add_options_to_select(){
               for (i = 0; i < 10; i++)
               { 
                    $('#mySelect').append( '<option value="'+i+'">'+'Option '+i+'</option>' );
               }
             }
             });
          </script>
          <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
          <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
       </head>
       <body>
          <div class="container body">
             <div class="col-md-12 col-sm-12  form-group has-feedback">
                <div class="input-field">
                   <i class="material-icons prefix">assignment</i>
                   <select id='mySelect'></select>
                   <label>Assignment</label>
                </div>
             </div>
          </div>
          <script>
             $('select').formSelect();
             
             $('.datepicker').datepicker();
          </script>
       </body>
    </html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It works fine if you initialize formSelect() after calling add_options_to_select();

<html>
       <head>
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <script src = "https://code.jquery.com/jquery-1.12.4.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
          <script>
             jQuery(document).ready(function($){
             add_options_to_select();
              $('select').formSelect();
             function add_options_to_select(){
               for (i = 0; i < 10; i++)
               { 
                    $('#mySelect').append( '<option value="'+i+'">'+'Option '+i+'</option>' );
               }
             }
             });
          </script>
          <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
          <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
       </head>
       <body>
          <div class="container body">
             <div class="col-md-12 col-sm-12  form-group has-feedback">
                <div class="input-field">
                   <i class="material-icons prefix">assignment</i>
                   <select id='mySelect'></select>
                   <label>Assignment</label>
                </div>
             </div>
          </div>
          <script>
            // $('select').formSelect();
             
             $('.datepicker').datepicker();
          </script>
       </body>
    </html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!