I clone the DIV element when I click the button, I can change the identifier value of the DIV element that I cloned. But is it possible to change the identifier of the inner element.
In the code below, I change the identifier #selection
when cloning, I need to dynamically change the identifier #select
.
<div id="selections">
<div class="input-group" id="selection">
<span class="input-group-addon">
<i class="icon wb-menu" aria-hidden="true"></i>
</span>
<select class="show-tick" data-plugin="select2" id="select">
<option>True</option>
<option>False</option>
</select>
</div>
</div>
<button class="btn btn-primary" type="button" style="margin-left: 30px;">
Add new selection
</button>
Js below
$(function() {
$("body").on("click", ".btn-primary", function() {
alert($(".input-group").length)
var
length = $(".input-group").length,
newId = "selection-" + length++,
clone = $("#selection").clone().attr("id", newId);
$("#selections").append(clone);
});
});
source
share