maxp
maxp

Reputation: 25161

Appending / Adding function to existing jqueryui DatePicker 'onSelect' event?

Im trying to append or add a function to the existing call stack for jqueryui's datepicker 'onSelect' event, but am failing miserably.

Ive pasted an example of what im trying to do here.

Basically, it should alert('abc'), then alert('def') after the first datepicker selection has been made.

http://jsfiddle.net/cfE7w/

Any advice appreciated.

Upvotes: 1

Views: 1946

Answers (1)

a better oliver
a better oliver

Reputation: 26838

After you called this

$('#hello').datepicker("option", "onSelect", function(){
    var prevFunction ...

the current event handler is already the new one! So when the event fires and the following code executes

var prevFunction = $(this).datepicker("option","onSelect");

prevFunction points to itself. You can solve this using an IIFE:

$('#hello').datepicker("option", "onSelect", 
  (function() {
    var prevFunction = $('#hello').datepicker("option","onSelect");
    return function(){
      prevFunction();
      alert('def');
   };
 }()));

Now var prevFunction = .. is called immediately and the old handler is stored. The new handler is assigned with return function(){ ....

Upvotes: 2

Related Questions