I am trying to implement an edit feature, which allows me to toggle back and forth between a text element and a form element. The current functionality of the attached code is correct, except that the edit button becomes non-functional when it re-appears after clicking "cancel". I used event delegation to get the created "cancel" button to function, but now I need to somehow circle it back to the top.
$(document).ready(function() {
$("#edit").click(function() {
var fruit = $("p").html();
var editbutton = $("#button").html();
$("#button").html("<input id='submit' type='button' value='submit' /><input id='cancel' type='button' value='cancel' />");
$("p").html("<input type='text' value='" + fruit + "' />");
$("#button").on('click', '#cancel', function() {
$("#button").html(editbutton);
$("p").html(fruit);
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p>Apple</p>
<span id='button'><input id='edit' type='button' value=edit /></span>