tags:

views:

14

answers:

2

I have been trying to figure out why this jQuery script calls the click bind event three times for every one actual click on the '.day-available' element. This is a calendar that updates a <div> next to the calendar.

Everything works fine, but I see in Firebug that each ajax url call is being executed three times. I have found information about using something like $(this).unbind, but I need to be able to allow the user to go back to a date on the calendar that they had clicked on previously.

The number of calls doesn't change between the first click and subsequent clicks.

Drupal.behaviors.uc_deliveries_now = function(context) {  
 $('.day-available').click(function(){
     $('.day-available').removeClass('selected-day');
     $(this).addClass('selected-day');

     var selected_day = $(this).html();
     var m_names = new Array("January", "February", "March","April", "May", "June", "July", "August", "September", "October", "November", "December");
     var d = new Date();
     var current_day = d.getDate();
     var current_month = d.getMonth();
     var current_year = d.getFullYear();

     if (selected_day < current_day) {
       // Move to next month
       current_month += 1;
     }

     $month_string = m_names[current_month];

     $.ajax({
       url: Drupal.settings.basePath + 'delivery_windows/' + $month_string.toLowerCase() + '/' + selected_day,
       success: function(data) {
         $('#time-selection').html(data);
       }
     });
   });
};

This is the only thing in the javascript file, there is no other bindings or anything else that could conflict with this. The whole javascript file is pasted above. I just cannot figure out why this is happening.

Here is a row sample from the calendar HTML:

<tr>
  <td>24</td>
  <td class="day-available">25</td>
  <td>26</td>
  <td class="day-available selected-day">27</td>
  <td>28</td>
  <td class="day-available">29</td>
  <td>30</td>
</tr>

Any help would be greatly appreciated.

A: 

try this

 $('.day-available').unbind()

before binding click event.

Chinmayee
A: 

add return false; to the end of the click handler.

tobyodavies