Fri
Feb
26
jQuery confirmation dialogs
The coolest jQuery snippet I wrote in a while (sorry about the wrapping):
$('.users form.delete, .user form.delete'
).submit(function(){
var name = jQuery(this).parents('.user-details').find('.user-name'
).text().replace(/^\s*(.*\S)\s*$/,'$1');
if(name==''){name='this';}
return window.confirm('Are you sure you want to delete '+
name+'?');
}).append('<a href="javascript:void(0)" '+
'class="delete_btn_surrogate">Delete</a>'
).children('input[type=submit]').hide();
$('form.delete a.delete_btn_surrogate').click(function(){
jQuery(this).parents('form').submit();
});
It looks for the name of the person (user) being deleted, in the markup, and inserts it into confirm() dialog box. This works on two completely different pages, the list of many people, and the specific page view for one person. It feels like I invented my own microformat.