Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are you sure you want to delete?

Tags:

jquery

I just wrote a confirm delete function that requires a class name:

jQuery(document).ready(function($) {
    $('.ConfirmDelete').click(function() {
        var question = $(this).attr('title');
        if (question == '') question = 'Delete this record?';
        return confirm(question);
    });
});

Usage:

<input name="Delete" type="submit" value="Delete" class="ConfirmDelete" title="Delete #UsrName#?" />

I'd like to change the selector from .ConfirmDelete to something like:

$('input:submit').attr('name','Delete').val('Delete')

Meaning: If a submit button has the name 'Delete' and it's value is 'Delete', then go ahead and assume they want to confirm the delete without requiring them to have a ConfirmDelete class.

like image 942
Phillip Senn Avatar asked Jan 01 '10 20:01

Phillip Senn


2 Answers

$(':submit[name="Delete"][value="Delete"]').click(function() {
    return window.confirm(this.title || 'Delete this record?');
});
like image 121
David Hellsing Avatar answered Nov 11 '22 19:11

David Hellsing


The following would apply to all present, and future instances of any submit button having both the name and value of "Delete":

$(function(){

    $(":submit[name='Delete'][value='Delete']").live("click", function(e){
      e.preventDefault(); // remove if not necessary
      // Seriously, delete it.
    });

});
like image 6
Sampson Avatar answered Nov 11 '22 19:11

Sampson