Thursday 29 April 2010

Assigning click event using jQuery in FireFox not working

I've just stumbled upon a strange problem which highlighted some badly formatted Javascript on my part, I had around 30 anchors on my page which needed to have Click event handlers assigned in order for me to override the href invocation, the reason for this was to prevent my Click from propagating through the underlying DIV element that the anchor was sitting in, so here was my original code:


$(document).ready(function() {
$(".addthis_button_facebook, .addthis_button_twitter").click(function() {
return click_addthis(event, this.href)
});
});


Which actually worked fine in every browser except FireFox, this was stumping me for ages until I decided to post a question at the wonderful Stack Overflow (what a god-send that place is!), which resulted in someone immediately spotting the missing argument in my function event.

The code should've been:


$(document).ready(function() {
$(".addthis_button_facebook, .addthis_button_twitter").click(function(e) {
return click_addthis(e, this.href)
});
});


If you haven't noticed it, it is the argument in the function that is required, this can then be passed onto the function being called.

I hope this helps someone who may have a similar issue/oversight, although it's probably just me and my limited JS experience!

So thanks again to the helpful chaps at stack overflow!