views:

278

answers:

2

Hi folks,

I have a script that generates a modal dialog and inside of that, I would like to put a jquery ui datepicker. I'm currently using the following code:

$('#datepicker').live('click', function() {
  $(this).datepicker({showOn:'both'}).focus();
});

Which works just fine the first time I open the dialog and click on the input box assigned the 'datepicker' id.

The problem is that after I close that dialog (which removes its markup from the DOM) and re-open it, the datepicker no longer works.

I have tried calling $('#datepicker').die() upon closing the dialog and then re-binding to the #datepicker input upon re-opening it, but that doesn't seem to work either.

I'm really stumped on this one. Any insight would be greatly appreciated.

Thanks!

A: 

My guess is that the removal of the triggering element from the DOM is your issue. Why not just hide your modal window and then show it when you need to? I believe that would take care of your issue.

Cryo
Unfortunately, that's not really an option, since the content that triggers the modal window also changes and the contents of the modal dialog vary based on the parent content+xml from the back end. It's just way easier to re-generate the dialog each time we need it. I know it sounds convoluted, and it sort of us, but there are some pretty strict confines I have to work within that are, sadly, beyond my control.You have given me an idea, though. I will try replacing the #datepicker input on the fly with one that already exists (hidden) within the DOM and see if that does the trick.Thanks.
Kelli Shaver
A: 

Turns out, using a class instead of an ID (i.e. '.datepicker' instead of '#datepicker') fixed the issue, even though there was only ever one occurance of #datepicker on the DOM.

Even then, it only works if there's another hidden input with the .datepicker class attached.

So it was sort of a combination of both suggestions that fixed it.

Thanks to both of you for the help!

Kelli Shaver