views:

540

answers:

2

(Hopefully) Simple question:

Is it possible to make the jQuery UI Datepicker draggable? If so, can someone give me some sample code?

+3  A: 

$(".ui-datepicker").draggable() does not work?

zalew
Indeed it does. I'm obviously new to jQuery UI so thanks for the help on a noobish question.
Andrew
no problem :) to know what classes and ids elements loaded via javascript have, install Firebug.
zalew
Oh believe me, Firebug is my friend. I just didn't know if it was even possible to make the datepicker draggable.
Andrew
+1  A: 

The order is important.

$('#myCalendarInput').datepicker();
$('#ui-datepicker-div').draggable();

EDIT: There is a subtle difference between the answer @JZ supplies and mine. Using JZ's example any datepicker on the page will be draggable, even those that are inline (associated with a DIV instead of an input element). My code will only make popup datepickers draggable. The datepicker plugin creates a single DIV (named ui-datepicker-div) for all popups and reuses it for any input on the page to which the datepicker has been applied. For inline DIVs or SPANs it creates a new, unnamed datepicker inside the DIV/SPAN that has the .ui-datepicker class, but does not have the name. In this case, my code will not make the datepicker draggable, arguably the correct behavior, but JZs will since it will match based on the class.

Example:

<script type="text/javascript">
$(document).ready(function() {
    $('#calendar').datepicker();
    $('#calendar2').datepicker();
    $('#calendar3').datepicker();
    $('#ui-datepicker-div').draggable();
});
</script>

<div>
Calendar: <input id="calendar" type="text" /><br />  <-- draggable
Calendar2: <input id="calendar2" type="text" /><br/> <-- draggable
Fixed calendar: <div id="calendar3"></div> <-- fixed
</div>
tvanfosson
I haven't tried this since JZ's answer worked, but wouldn't you run into a problem with multiple datepickers on the same page with this solution?
Andrew
There is one difference between my code and JZ's. For an input element, datepicker will only create a single ui-datepicker-div and reuse it for all inputs that require a popup. If you use an inline calendar, though, JZ code will make it draggable whereas mine won't since it won't have the same id.
tvanfosson
Ah. Thanks for the explanation. +1
Andrew