JSFiddle - React, Tailwind, and code Playground
by dimaslanjaka
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/css/select2.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/js/select2.min.js"></script>
<div id="contents">
<select id="sel" data-placeholder="-Select education level-">
<option></option>
<option value="a">High School</option>
<option value="b">Bachelor</option>
<option value="c">Master's</option>
<option value="c">Doctorate</option>
</select>
</div>
<br>
<button id="add">add a dropdown</button>
CSS
#contents div.select2-container {
margin: 10px;
display: block;
max-width: 60%;
}
JavaScript
$(document).ready(function () {
console.clear()
$("select").select2();
$("#add").click(function () {
$("#contents")
.find("select")
// call destroy to revert the changes made by Select2
.select2("destroy")
.end()
.append(
// clone the row and insert it in the DOM
$("#contents")
.find("select")
.first()
.clone().select2()
);
// enable Select2 on the select elements
$("select").select2();
});
});