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>