i have tried many resources :
Cloned Select2 is not responding
Initialising select2 created dynamically
How to execute select2() function on dynamically created select list?
Select2 not displayed when added dynamically for the first time
Explanation:
View page is build up using
Razor
. i'm trying to clone therow
using JQuery, first one is working properly and newly created( dynamic) is not opening (freezed). Image: image link
View Code:
<div id="ROW_0" class="template">
<hr />
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Credit Account</label>
@Html.DropDownListFor(model => model.AccountsId, new SelectList(Model.Accountss, "Id", "Name"), "-- select account --", new { @class = "form-control select required select2insidemodal", @id = "creditAccount" })
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Amount</label>
@Html.EditorFor(model => model.Credit, new { htmlAttributes = new { @class = "form-control", @type = "text", @id = "credit" } })
</div>
<span id="error-@Html.NameFor(vm => vm.Credit)" class="dN cR"></span>
</div>
</div>
</div>
Script:
//button for adding row
$(document).ready(function () {
$(document).on('click', "#addMore",function (e) {
addNewRow();
});
function addNewRow() {
var div = $(".template").clone().html();
//find all select2 and destroy them
$(div).find(".select2").each(function (index) {
if ($(this).data('select2')) {
$(this).select2('destroy');
}
});
$("#dynamicBlock").prepend(div); //adding cloned data to new div '#dynamicBlock'.
Help me out.