I have a strange problem with a delete button in a jQuery dialog box. What should happen is that once the delete button is clicked, it looks up the value of the hidden input button and sends that value to an ajax call to delete the content. So every click should only show the single id that belongs to the last clicked button.
What actually happens with the first click it that it shows the correct value. But when you then click the next button, it will first show the previous id and then the new id, so the ajax call is made twice. If you would click on a third button, it will show three ids, and make three ajax calls which is not what should happen. It should stick with a single ajax call with each click and only show the latest id.
You can see an example here http://jsfiddle.net/uF5fX/11/, with the ids shown in the console. Any ideas on what I'm doing wrong, and how to fix this?
$("#item-list").on( "click", ".delete-item", function( e ) {
var dialogBox = $("#delete-confirmation"),
storeId = $(this).next('input[name="store_id"]').val();
console.log( 'main clicked store id = ' + storeId );
dialogBox.dialog({
width: 325,
resizable : false,
modal: true,
minHeight: 0
});
$(".ui-dialog-titlebar").remove();
dialogBox.find(".button-secondary").on( "click", function() {
dialogBox.dialog("close");
});
dialogBox.find(".button-primary").on( "click", function( elem ) {
console.log( 'click delete btn' );
console.log( 'ajax store id = ' + storeId );
dialogBox.dialog("close");
//make a singe ajax call with the last storeId
//elem.stopImmediatePropagation();
elem.stopPropagation();
elem.preventDefault();
return false;
});
e.stopPropagation();
e.preventDefault();
return false;
});
Structure of the html
<ul id="item-list">
<li>
<input type="button" value="Delete" name="text" class="delete-item" />
<input type="hidden" value="60" name="store_id" />
</li>
</ul>
Normally when multiple clicks are triggered it can be fixed with return false, or preventDefault / stopPropagation but it makes no difference here?