tags:

views:

65

answers:

2

Hello,

I am trying to do the following:

$("#something ."+id).click(function() {
    callFunction(id,message);
});  

$("#something ."+id).silentclick(function() {
    callFunction(id,message,'silent');
});

But silentclick gives an error saying function does not exist. How do I go about creating my own custom event like silentclick?

Thank you for your time.

+2  A: 

You extend jQuery like this:

(function($){
    $.fn.silentclick = function(action) {
        /* do what you like with the function passed */
        action(); /* for example invoke the function */
    }
})(jQuery);

Without knowing what silentclick wants to do or how you want it triggered, I can't really give any more detail.

Garry Shutler
+3  A: 

I think you are trying to create a custom event. If this is the case, you can use either .bind() or .live().

$("#something ."+id).bind('silentclick',function() {    
  callFunction(id,message,'silent');
});

Then, you could fire the event from somewhere else in your code.

$("#something ."+id).trigger('silentclick');
ichiban
Damn, you beat me to it by 30 seconds :(
Pim Jager