I have created two buttons dynamically that will appear on button click, but the buttons aren't appearing which I believe from the research I have done is due to the the buttons trying to be added after page load. I then tried to use $('body').on('click', '.edit', edit); to add the buttons as what was explained here.
$(document).ready(function() {
$('body').on('click', '.edit', edit);
function edit() {
var edit = $(this);
var value = edit.val();
edit.hide();
var cancelBtn = document.createElement('button');
cancelBtn.type = "button";
cancelBtn.name = "Cancel";
cancelBtn.className = "btn btn-primary cancel";
cancelBtn.style.visibility = 'visible';
var saveBtn = document.createElement('button');
saveBtn.type = "submit";
saveBtn.value = value;
saveBtn.name = "Save Changes";
saveBtn.className = "btn btn-primary save";
saveBtn.formAction = '@Url.Action("Edit")';
saveBtn.style.visibility = 'visible';
$(".cancel").click(function cancel() {
edit.show();
document.getElementById("save").style.visibility = "hidden";
document.getElementById("cancel").style.visibility = "hidden";
});
};
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" class="edit">Edit</button>
<button type="button" class="cancel">Cancel</button>