I'm trying to write a simple jQuery plugin to show an alert box (custom html + css) and bind the yes an no buttons to functions passed as an argument of the plugins call.
Here's my code so far:
(function($) {
$.fn.triggerAlert = function (msg,yes,no) {
$('#alert_mask').find("span:first").html(msg);
$('#alert_mask').toggle();
$('#alert_mask #alert_yes').click(function (yes) {
if (typeof yes == 'function') {
yes($(this));
}
$('#alert_mask').hide();
return false;
});
$('#alert_mask #alert_no').click(function (no) {
if (typeof no == 'function') {
no($(this));
}
$('#alert_mask').hide();
return false;
});
}
})(jQuery);
Is this in the right track or is it just plain wrong?
Thanks
UPDATE: after Logan F. Smyth answer I had to make an ajustment because the click events of the yes and no buttons here being defined several times. For future reference or for someone else benefit here is the complete plugin.
(function($) {
$.fn.triggerAlert = function (trigger,msg,yes,no) {
var mask = $('#alert_mask');
$(trigger).click(function (e) {
mask.find("span:first").html(msg);
mask.toggle();
e.preventDefault();
});
$('#alert_yes').click(function (e) {
if (yes) yes($(this));
mask.hide();
e.preventDefault();
});
$('#alert_no').click(function (e) {
if (no) no($(this));
mask.hide();
e.preventDefault();
});
}
})(jQuery);
And an example of how to call it
$().triggerAlert(
$('#some_element'),
'hello world',
function() { alert('yes') },
function() { alert('no') }
);