I have a feed with comments and want to allow users to delete a comment. The code below allows users to click on an image and a dialog box appears that alerts users of the deletion.
Right now, this applies to every comment on the page, so that clicking the delete button once opens up multiple dialogs (as many dialogs as there are comments).
How can I alter the code below so that when the selector is clicked, only the dialog box for that comment appears?
$('span.delete_comment_button img').click(function() {
$('.delete_comment_dialog').dialog('open');
return false;
});
First of all, you should not have multiple
#delete_comment_dialogelements on the page so we’ll change that to.delete_comment_dialog. Then you can add a class to the comment as a whole, useclosestto go up to the top level comment wrapper, andfindto come back down to the dialog. The HTML would look something like this:And your jQuery like this:
Demo: http://jsfiddle.net/ambiguous/VePZp/
Alternatively, use a single dialog with an
idattribute, addids to your comment<div>s, and pass theidof the comment to delete through adataattribute or similar. For example:And:
Demo: http://jsfiddle.net/ambiguous/M4QM6/