I need to develop a form that allows users to edit existing comments. The form will display a textarea containing the old comment text and a submit button. My goal is to send the newComment data via ajax to another script. However, I am facing an issue where the alert message only shows the old comment instead of the new one. Can someone provide me with some insight on what might be causing this? It seems like the button is not properly submitting the data, as when I set it to type submit, the website reloads which is something I want to avoid.
function editComment(id, text){
var commentId = "#" + id;
$(commentId).replaceWith("<form>\n\
<textarea id='newComment'>" + text + "</textarea>\n\
<input type='button' id='submit' value='save'>\n\
</form>");
$('#submit').click(function() {
var newComment = document.getElementById('newComment').innerHTML;
alert(newComment);
$.ajax({
type: 'get',
url: '/editComment',
data: "newComment",
});
});
}