'I want to generate select2 on button click

I'm adding select2 element like this

//HTML Code
<html>
<select id="select2Basic" class="select2 form-select form-select-lg">
 <option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 2 </option>
</select>

<select id="select3Basic" class="select2 form-select form-select-lg">
 <option value="4"> 4 </option>
<option value="5"> 5 </option>
<option value="6"> 6 </option>
</select>
</html>


//Jquery
<script>
$("#select2Basic").select2(); //initialise select2 using id
$("#select3Basic").select2();
</script>
// I can't use same id on multiple element, that wont work.

Now i want a button that generate select2 element when clicked



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source