jQuery passing click selector into IF statement

466 views Asked by At

I am trying to create a jQuery function to check whether a Cancel or Remove button was pressed on a facebox dialog, however I am not quite sure how to go about it.

Right now, I have:

// Confirm and remove group members
$("[id^='removeGroupMember_']").click(function () {
        confirmDialog('Removal', 'Are you sure you want to remove this person from the group?');

        //  I need to check the results of the confirm dialog prior
        //  to calling the code below to remove the actual rows

        $(this).parent().slideUp("fast", function () {
            $(this).remove();
            updateGroupRows();
        });
    return false;
});

Where confirmDialog is:

function confirmDialog(action, message) {
    $.facebox('<h3 class="confirmHeader light tb">Confirm ' + action + '</h3><div class="confirmContent"><p>' + message + '</p><a href="#" id="dialogConfirmAction" class="ras small red button right">' + action + '</a><a href="#" id="dialogConfirmCancel" class="ras small gray button right">Cancel</a></div>');
};

Right now, I have two functions for when those buttons are pressed, but I'm not sure how to check their result and feed that back in so I can decide whether or not to delete the associated row:

$('#dialogConfirmAction').live('click', function() {
    console.log('Yep... they dun clicked it.');
    return true;
});

$('#dialogConfirmCancel').live('click', function() {
    $.facebox.close();
    return true;
});

Any guidance you can provide is greatly appreciated!

2

There are 2 answers

3
Richard Marskell - Drackir On BEST ANSWER

What you want to do is change your confirmDialog function to be like so:

function confirmDialog(action, message, actionfunc) {
    $.facebox('<h3 class="confirmHeader light tb">Confirm ' + action + '</h3><div class="confirmContent"><p>' + message + '</p><a href="#" id="dialogConfirmAction" class="ras small red button right">' + action + '</a><a href="#" id="dialogConfirmCancel" class="ras small gray button right">Cancel</a></div>');
    if(actionfunc) {
        $('#dialogConfirmAction').click(actionfunc);
    }
};

You can then pass what you want to happen "on action" by passing a function to the confirmDialog function. This would make your other code look like this:

$("[id^='removeGroupMember_']").click(function () {
    var $that = $(this);
    confirmDialog('Removal', 'Are you sure you want to remove this person from the group?',
                  function() {
                      //This function will be run when the "action" link is clicked
                      $that.parent().slideUp("fast", function () {
                          $(this).remove();
                          updateGroupRows();
                      });
                  });
    return false;
});

And you can extend that by adding another variable to say what to do on cancel.

1
Steve On

Try this:

 // Confirm and remove group members
$("[id^='removeGroupMember_']").click(function () {

        if(confirmDialog('Removal', 'Are you sure you want to remove this person from the group?')) {
            //  I need to check the results of the confirm dialog prior
            //  to calling the code below to remove the actual rows

            $(this).parent().slideUp("fast", function () {
                $(this).remove();
                updateGroupRows();
            });
        }
    return false;
});