I have created a script so far that detects when the class object .edit has been clicked and then dynamically creates two buttons to display. What should happen is that only the row that the button was clicked in should then display the buttons, but right now it displays the buttons in all the rows. This is due to having to loop through the model and display all the data entries. I have thought of using unique ids but I don't quite understand how to use this method or if there is a better way to perform this action.
The for loop that I created in my javascript the original thought behind that as well was to loop through all the entries, but only display the button one time out of the entire loop, but still resulted in all the rows showing the button.
@foreach (var item in Model.Records){
<tr id="@item.RowIndex">
<td>
<button type="button" data-id="{{@item.RowIndex->id}}" id="triggerEditing" value="@item.RowIndex" class="btn btn-primary edit">Edit</button><br /><br /><br />
<p class="dummy_field" data-id="{{@item.RowIndex->id}}" id="dummy_field"></p>
</td>
</tr>
}
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('body').on('click', '.edit', edit);
function edit() {
var edit = $(this);
var value = edit.val();
edit.hide();
$('select')
for (var i = 0; i < $('.edit').length; i++) {
if (i = 1) {
var cancelBtn = ('<button class="btn btn-primary cancel">Cancel</button>');
$('#dummy_field').append(cancelBtn);
var saveBtn = ('<button class="btn btn-primary save" id="save">Save Changes</button>');
$("#save").attr('value', value)
$('#dummy_field').append(saveBtn);
break;
}
}
$(".cancel").click(function cancel() {
edit.show();
document.getElementById("save").style.visibility = "hidden";
document.getElementById("cancel").style.visibility = "hidden";
});
});
</script>