So I am executing a $.post() to get some html code returned from a php script which accesses an SQLite database
I need to attach a click event to an edit button that shows when the page loads the new html, so I attach the click event after the .empty().html() then if the user edits the record by submitting the data it does another $.post() to reresh the screen and show the new data, so I attach the click event handler again, because if I don’t the edit buttons on the page don’t work anymore because they weren’t there when the `.click()’ was attached the first time
BUT when I do this and edit multiple records it runs multiple times (I alert the record ID each time it does a $.post() and it does it the same amount of times as edits I have clicked, I have seen posts about how .live() can cause this issue and people suggest binding click which is what I am doing, so I don’t see what’s happening…here is the code
$(".edit").click(function(e){
e.preventDefault();
$realTipID=this.id;
$.post("ajax.php",{action:'getTip',tipID:$realTipID},function(data){
$("#editTip_div").css("display","block");
$("#editText").empty().val(unEscapeSpecialChars(data));//puts the data that is currently in the DB into a textarea to be editted
$("#edit_tip").click(function(e){
$.post("ajax.php",{action:'editTip',tipID:$realTipID,editText:escapeSpecialChars($("#editText").val())},function(data){
if (data=="1") {//check tosee that the update of the SQL table worked
e.preventDefault();//tried this because someone suggested it in another StackOverflow post
$(".edit").unbind('click');//and this
$realTipID='';
$("#editText").val('');
$("#editTip_div").css("display","none");
$('.result[id|='+$issueID+']').click();//refreshes the <div> with the content, includes this $(".edit").click() event
if (data=="0") {//or failed
alert("record not written, could be that there are quotes in the text, please remove, quotes, apostraphes, and ticks (`) as they will cause the line to fail");
}
});
});
$("#cancel_edit").click(function() {
$("#editText").val('');
$("#editTip_div").css("display","none");
});
});
});
Sounds like you’re setting multiple event handlers on the same element. Does this help?
edit: sorry, that should have been:
Edited from: