goal
I have a webpage with a table of elements. Each item has a delete button next to it. When this button is pressed, I want
- Ask user to confirm
- Delete the corresponding item from the database
- Remove this line from the list.
Current solution
Now I am doing something like this:
$('button.delete').click(function(){ thisRow = $(this).parent(); itemID = $(this).parent().attr('id'); if (confirm('Are you sure?')){ $.post('/manage_items.php', {"action":"delete", "itemid":itemID}, function(){ thisRow.hide("slow").remove(); }); } }
This solution works because each button.delete can determine which line and element it belongs to and act accordingly.
Desired Solution
Instead of the clumsy OK or Cancel window, I would like to use the jQuery UI dialog box. But I'm not sure how to tell the dialog which line and element it should process each time it is clicked.
Here's how you set it up:
1) Define the div dialog
<div class="dialogbox" id="confirmdeleteitem" title="Really DELETE this item?"> <p>Gee golly, are you sss-sure you want to do that?!</p> </div>
2) Customize the behavior of the dialog box
$('#cofirmdeleteitem').dialog({ //other options - not relevant here buttons: { "Nevermind": function() { //do nothing }, "Alright! Woo!": function(){ //do something } } });
3) Define a click event that will open a dialog
$('button.delete').click(function(){ $('#confirmdeleteitem').dialog('open'); });
At this last stage, I would like to pass some information in the dialog box - for example, the delete button was pressed. But I do not see a way to do this.
I could insert a hidden dialog div.dialog into each line of the element up or insert it into a specific line after clicking on the button. Then the links $(this).parent() would capture the correct line ...
Is there an easier way to do this?