Pages

Wednesday, 13 March 2013

Custom jQuery events

I'd been working on some javascript elements recently when I realised I needed to create an interaction or call between two different scripts. These areas would potentially be absent and if present would need data pushed. After discounting some global variable registration ideas, I found that custom jQuery events would fit the bill perfectly.

The following is code for both the event declaration and the triggers.
// do this when triggered
$.bind('onMySelect', function(event, argData){
 console.log(argData);
});

// the trigger
$.event.trigger('onMySelect', 'my test data');
One additional thing seems to be that, if the event declaration isn't present then it silently fails without errors (at least in Firefox my primary development browser of choice).

No comments:

Post a Comment