My script works fine on static content, but breaks when the content is changed via AJAX - jQuery -


i writing custom calendar, consists of 2 parts, 1 div showing month grid, , second div showing events selected date. current month's calendar grid loads automatically, , display of daily events works correctly. however, when user clicks button view next or previous month, clicking on dates no longer works. also, changing month no longer works on dynamically-loaded calendar.

this jquery code using:

<script type="text/javascript">     $(document).ready(      function() {         $('div.calendar_events').text('to view events on of highlighted dates, click date.');         $('div.number').click(          function(e) {             e.preventdefault();             var thedate = $(this).attr('id').split('-');             $('div.calendar_events').load('ajax/calendar_events.php?month=' + thedate[1] + '&day=' + thedate[2] + '&year=' + thedate[3]);         });         $('a.changemonth').click(          function(e) {             e.preventdefault();             var thedate = $(this).attr('id').split('-');             $('div.calendar').load('ajax/calendar_view.php?month=' + thedate[1] + '&year=' + thedate[2]);         });     }); </script> 

don't setup events directly using click. use live instead.

$('div.number').live('click', function() { .. }); 

when new ajax content loads, new content replaces old , wipes out event handlers attached replaced content using bind or shortcut methods such click, change, etc.

live event handlers, on other hand, added document , due event bubbling in dom, events reach document (which never replace, or ideally should never replace :), can think of document safe-haven adding events.

also checkout delegate way make use of event bubbling, if performance freak.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -