Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add additional event handlers to a jquery-ui datepicker instance?

I've got a datepicker with an onSelect event handler. Later in my program, I want to add other onSelect handlers. Problem is, they wipe out the first one. (Fiddle.)

How can I add additional handlers without doing that?

like image 320
sprugman Avatar asked Nov 14 '22 11:11

sprugman


1 Answers

You could maintain your desired calls in an array;

var firstHandler = function(dateText, inst) {
    console.log('Original Handler', dateText, inst);
};

var secondHandler = function(dateText, inst) {
    console.log('Second Handler', dateText, inst);
};

$('#datepicker').datepicker({
    onSelect: function() {
        var sinks = $(this).data("mySelects");
        for (var i = 0; i < sinks.length; i++)
            sinks[i].apply(this, arguments);
    }
}).data("mySelects", [firstHandler]);

$('#addBtn').click(function(event) {
    $('#datepicker').data('mySelects').push(secondHandler);
    $('#datepicker').data('mySelects').push(nthHandler);
});
like image 63
Alex K. Avatar answered Dec 08 '22 08:12

Alex K.