I have following razor code. I need to add this dynamically on button click in client side.
<div class="form-group" id="transitem" name="transitem">
<div class="col-md-11" id="tritem" name="tritem">
@Html.LabelFor(model => model.transaction_item_id, "transaction_item_id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-3">
<div id="trans_dd_1" name="trans_dd_1">@Html.DropDownList("transaction_item_id", null, htmlAttributes: new { @class = "form-control", @id = "trans_id_#", @Name = "trans_id_#" })</div>
@Html.ValidationMessageFor(model => model.transaction_item_id, "", new { @class = "text-danger" })
</div>
<div>
@Html.LabelFor(model => model.transaction_itmQty, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-3">
@Html.EditorFor(model => model.transaction_itmQty, new { htmlAttributes = new { @class = "form-control", @id = "trans_qty_#", @Name = "trans_qty_#" } })
@Html.ValidationMessageFor(model => model.transaction_itmQty, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-2"><input type="button" class="btnPlus" value="+" /></div>
</div>
</div>
I have written following script, but it is not working as expected. I need to change the name and id of trans_id_# and trans_qty_#. They are being generated by html helper, html.editorfor. Below is my script, which can copy the new element, but i am not able to change the id or name.
<script type="text/javascript">
var itm_cnt = 1;
$(document).ready(function () {
$(document).on("click", ".btnPlus", function () {
var new_clone = $('#tritem').clone();
itm_cnt = itm_cnt + 1;
var new_name = "trans_id_" + itm_cnt.toString();
$(new_clone).attr("name", "new_name");
$('#transitem').append(new_clone);
window.alert(itm_cnt);
});
});