0

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')  }
  );
1
  • Not as I expected. I tested passing the alert function as arguments, and the alert is called as I call the plugin instead of in the click functions of the buttons Commented Dec 14, 2011 at 2:08

2 Answers 2

2

The main issue I see is that your click handlers take 'no' and 'yes' arguments, which means that inside of those functions, yes and no won't be what you passed to the overall plugin.

It's also unneccesary to have your selects use two ids, since ids are unique anyway. Finally return false is a bad idea, use preventDefault instead.

(function($) {
  $.fn.triggerAlert = function (msg,yes,no) {
    var mask = $('#alert_mask');
    mask.find("span:first").html(msg);
    mask.toggle();

    $('#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);

To trigger this, you would call the function and pass two functions like this:

$('#some_element').click(function(e){
  $(this).triggerAlert(
    'hello world',
    function() { alert('yes') },
    function() { alert('no')  }
  );
  e.preventDefault();
})
Sign up to request clarification or add additional context in comments.

7 Comments

Then how to I passed the functions to the click handler. If I don't set them as arguments the they are undefined when the click appends
A function can see all the variables in the functions that wrap around it, so they can see 'yes' and 'no' automatically. I've added some code to my answer. Also the 'this' passed to your callback is going to me the #alert_yes and no elements, not mask. is that what you want?
Thank for you interest but still the behavior is the same. and there is a part of your code that is not what I need. You're hiding and toggling an inner span that has the message, and I have to toggle an outter div.
You are right, the toggle part was incorrect. I've fixed it now. What other part isn't working? Can you show how you are actually calling this function with the arguments and such?
In that, you are executing both alert functions, and then passing the result of those functions to your triggerAlert code. You need to pass functions that, when executed, will trigger the alerts. I have updated my answer with an example. Also, don't use return false.
|
0

parameters are in the function scope, you can try like this

  $('#alert_mask #alert_no').click(function() {
    if (typeof no == 'function') { 
        no($(this));
    }
    $('#alert_mask').hide();
    return false;       
});

3 Comments

When the click happens no is undefined
How do you do initialization with triggerAlert
$('#some_element').click(function(){$(this).triggerAlert('hello world',alert('yes'),alert('no')); return false;})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.