5 Sep 2012

IE, jquery, migrating from live to on for click events

Had a bit of "fun" today migrating from using jquery live() to on(). I had a bit of code like this:

$('.BizCard.selectable').live('click', function () { blah; });

Knowing that we should now use the on() function instead, I changed it to:

$(window).on('click', '.BizCard.selectable', function () { blah; });

This seemed to work OK until I got a bug report that it wasn't working in IE8 and lower. Took a while to work it out, but turns out you can't attach a click handler to the window object in IE before IE9. Therefore the workaround was to attach to document instead:

$(document).on('click', '.BizCard.selectable', function () { blah; });

No comments:

Post a Comment

Comments are very welcome unless you're a spammer, in which case you should probably kill yourself.

If I helped you out today, you can buy me a beer below. Cheers!