I am simply creating a select list dynamically on a button click and appending it to a div. It is working without a problem but when I want this select list to behave like searchable as expected for select2 it is not working.
In below, I'm passing my select list into a method with an html helper named GGroupDropDownListForJavascript and the html result is like below.
<select class="form-control input-xxlarge select2me" id="TagCategoryId" name="TagCategoryId">
<option value="cf1d7da6-f49f-47aa-ba6d-a58f017c59ec">Element1</option>
<option value="cf1d7da6-f49f-47aa-ba6d-a58f017c59ec">Element2</option>
</select>
Here my Js code.
$('#post').click(function (e) {
var arrowBox = $( "<div>"+'@Ajax.JavaScriptStringEncode(Html.GGroupDropDownListForJavascript(p => p.TagCategoryId, Model.GroupedTagCategories, "").ToHtmlString())' + "</div>");
arrowBox.appendTo($("#imageContainer"));
$('#TagCategoryId').select2();
})
I m getting below error for this method.
Uncaught TypeError: $(...).select2 is not a function
I tested it for non-dynamically created elements btw and it is working without any problem. But when it comes to dynamically cretaed one it stops working. Am I missing something? Thank you.