HTML
xxxxxxxxxx
1
<div class="card p-4 mt-3 my-2">
2
<div class="card-body">
3
<h5 class="card-title mb-3">SELECT UPDATE</h5>
4
<form enctype="multipart/form-data" method="post" action="/Dev/Test">
5
<div class="form-row">
6
<div class="col-4">
7
<label class="mb-0">Options</label>
8
<select id="upd-select" class="mdb-select">
9
<option value="1">Option 1</option>
10
</select>
11
</div>
12
</div>
13
<div class="d-flex justify-content-start">
14
<button id="add-btn" type="button" class="btn btn-sm btn-primary waves-effect waves-light m-0">Add options</button>
15
</div>
16
<br>
17
<small class="text-muted">Press the button more than 3 times and inspect the select wrapper to see the error, anyway you browser are going to crash</small>
18
</form>
19
</div>
20
</div>
CSS
1
1
JS
xxxxxxxxxx
1
$(document).ready(function(){
2
$(".mdb-select").materialSelect();
3
});
4
5
$("#add-btn").click(function(){
6
$(".mdb-select").materialSelect({destroy: true});
7
let length = $("#upd-select option").length + 1;
8
$("#upd-select").append(new Option("Option " + length, length));
9
length++;
10
$("#upd-select").append(new Option("Option " + length, length));
11
length++;
12
$("#upd-select").append(new Option("Option " + length, length));
13
14
//I know append 1 option per time is not the best way to add options, but this isn't the problem here
15
//let options = [];
16
//for (var i = 0; i <= 3; i++) {
17
// options.push(new Option("Option " + length, length));
18
// length++;
19
//}
20
//$("#upd-select").append(options);
21
22
});
Console errors: 0