Sacrificial Lambda

Dynamically binding event handlers to content that has been ajaxed in with $.load without repeating yourself can be tricky. Lambda functions help you to not repeat yourself as much. jQuery uses lamdba functions everywhere, so if you're familiar with jQuery, you should be familiar with the syntax of lambda functions.

In this example, the ajaxed-in content contains the elements that trigger a $.load over themselves.

[js]prepare_links = function() { var month = $('.calendar-info .month').html(); var year = $('.calendar-info .year').html(); $('.previous-month').click(function() { $('#calendar').load(this.href, prepare_links); return false; }); $('.next-month').click(function() { $('#calendar').load(this.href, prepare_links); return false; }); }; $(document).ready(function() { prepare_links(); }); [/js]

Make sure you're not calling the function in the callback by accidentally using prepare_links():

[js] $('.next-month').click(function() { $('#calendar').load(this.href, prepare_links()); /* Don't do this */ return false; }); [/js]

This would set the callback to whatever the return value of the function is, not to the function itself.