I am using jQuery .replaceWith method to enable users to reply or cancel commenting on a post. The reply button works as it creates a text box when clicked. However, clicking the cancel button refreshes the page instead of going back to the original post without page reload. How can I make the cancel button function properly? Thank you for your help!
<form method="post">
<input type="hidden" name="post_id" value="{{ p.key.id() }}">
<input type="hidden" name="user_id" value="{{ user.key.id() }}">
<input type="button" name="reply" id="{{ p.key.id() }}" value="Reply">
</form>
<script>
$("#{{ p.key.id() }}").click(function() {
var reply = $("<textarea name='reply_content' style='resize: none; width: 550px; height: 100px;'></textarea><br><input type='submit' name='reply' value='Reply'><input type='submit' id='cancel_{{ p.key.id() }}' value='Cancel'>")
$("#{{ p.key.id() }}").replaceWith(reply);
});
$("#cancel_{{ p.key.id() }}").click(function() {
var cancel = $("<input type='button' name='reply' id='{{ p.key.id() }}'' value='Reply'>");
$("#{{ p.key.id() }}").replaceWith(cancel);
});
</script>