1

I am trying to add an option to a select element in html from Jquery. I used a form to submit the name of the category to be added to the select element. The new category option shows when inspecting the code, however, it is not showing in the document.

$(document).ready(function () {
  // initialization
  $('select').formSelect();

 $('#myForm').submit(function (e) {
  let category = $('#in').val();
  $('#selection').append(`<option value="${category}">${category}</option>`);
  e.preventDefault();
 })
});
<!DOCTYPE html>
<html>
<head>
 <!--Import Google Icon Font-->
 <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
 <!-- Compiled and minified CSS -->
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
 <!--Let browser know website is optimized for mobile-->
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />

 <title>My Lab</title>
</head>
 <body>
<div class="container">
 <form id="myForm">
 <div class="input-field">
 <label for="in">Enter category</label>
 <input type="text" id="in">
 </div>
 <select id="selection">
 <option value="" selected disabled>Category</option>
 <option value="1">1</option>
 </select>
 <button class="btn" type="submit">Submit</button>
 </form>
</div>
 </body>
<!--Import jQuery before materialize.js-->
 <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
 <script src="app.js"></script>
 </html>

1 Answer 1

1

The issue is because you're using a plugin which rebuild the select element in HTML which allows you to style it as you like. This means that any new options appended to the select after initialisation are not detected.

To fix this, call formSelect() again in your form submit event handler:

$(document).ready(function() {
  $('select').formSelect();

  $('#myForm').submit(function(e) {
    e.preventDefault();
    let category = $('#in').val();
    $('#selection').append(`<option value="${category}">${category}</option>`);

    $('select').formSelect();
  })
});
<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-beta/css/materialize.min.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>

<div class="container">
  <form id="myForm">
    <div class="input-field">
      <label for="in">Enter category</label>
      <input type="text" id="in">
    </div>
    <select id="selection">
      <option value="" selected disabled>Category</option>
      <option value="1">1</option>
    </select>
    <button class="btn" type="submit">Submit</button>
  </form>
</div>

It should be noted, however, that the user experience for this is rather odd. You have to type in the field, then submit the form (which seems to do nothing) and then the select which is above the submit button has it's options changed. It's not very intuitive.

Sign up to request clarification or add additional context in comments.

2 Comments

I just copied a portion of my actual program I was writing to minimise space here.
No problem, glad to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.