Search code examples
javascriptjqueryjquery-ui-datepicker

unable to bind click event for all columns headers. works only one time


I am looking for help why this code is not working for all headings of column. This code works for fist time for any column but does not fire click event second time. why $(ColTh).click(function () is not being attached to all th in header ?

$("table.ui-datepicker-calendar > thead").find("th[scope='col']").each(function() {
  var ColTh = $(this);

  $(ColTh).click(function() {
    var colIndex = $(ColTh).index();
    var dateList = [];
    $(ColTh).closest('table').find('tr').each(function() {
      var dateTd = $(this).find('td:eq(' + colIndex + ')');
      var month = $(dateTd).attr('data-month'); //.trigger('click');
      var year = $(dateTd).attr('data-year');
      var day = $(dateTd).children(0).text();
      if (typeof month != 'undefined') {
        var monthINT = parseInt(month.trim());
        monthINT = monthINT + 1;
        var newDate = monthINT + '-' + day.trim() + '-' + year.trim();
        dateList.push(newDate);
      }

    });
    $('#itemSchedulerCal').multiDatesPicker('addDates', dateList);
    dateList = [];

  });

});

Solution

  • i did following steps to achieve above

    Step 1:

    jQuery(document).ready(function () {
        $.datepicker._updateDatepicker_original = $.datepicker._updateDatepicker;
        $.datepicker._updateDatepicker = function (inst) {
        $.datepicker._updateDatepicker_original(inst);
        var afterShow = this._get(inst, 'afterShow');
        if (afterShow) {
            // trigger custom callback
            afterShow.apply((inst.input ? inst.input[0] : null));  
        }
    }
    

    });

    step 2:

       $('#itemSchedulerCal').multiDatesPicker({
            showWeek: true,
            minDate: 0,
           dateFormat: "m-d-yy",
        dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
        defaultDate: defdate,
        afterShow: function (inst)
        {
            BindClicks();
        }
    });
    

    Step 3:

    function BindClicks() {
    // paste all above code
    }