views:

24

answers:

2

Hi all,

I need to make the datepicker show when I click on a link and then send the selected date to a different page through a post call.

T tried to use this code for the link call:

$(".click-on-link").click(function(){
    $('#datepicker').datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'dd/mm/yy',
        firstDay: 1
    });
});

and the html:

<a class="click-on-link" href="#">show datepicker</a>

but it's not working. Any idea?

Thanks!

A: 

I'd start off by first initialising your date picker when the page loads as follows:

$(document).ready(function() {
     // Date picker initialisation
     $("#datepicker").datepicker({
         showOn: 'button',
         buttonImage: 'images/button_cal.gif',
         buttonImageOnly: true,
         dateFormat: 'd MM yy',
         minDate: new Date(),
         onSelect: function(dateText, inst) {
                    // inst.selectedYear, selectedMonth and selectedDate hold the values you need. Use $.ajax() to send off to the server.
         }
     });

     // Show the date picker
         $(".class").click(function() {
         $("#datepicker").datepicker("show");
     });

});

Try the following sample, which lets you click on the text 'Date' which brings up the date picker for me. Perhaps we can work from this sample to get your scenario working:

<script type="text/javascript">
$(function() {
    $("#datepicker").datepicker({});

    $("#test").click(function(){
        $("#datepicker").datepicker('show');
    });
});
</script>

<p id="test">Date:</p> <input type="text" id="datepicker">
Alistair
a want to show it when i click on a link, not on an image, as in:<a href="#" class="click-on-link">show datepicker</a>
Alex
Sorry that was some leftover sample code from other project. Have a look at the bit where I had // show the date pickerHopefully the onSelect bit helps you get access to the date and sending off via AJAX
Alistair
nope, it's not working, in the sense that it's not showing the datepicker div.
Alex
I've added another code sample to the answer, try that as it works for me to show a date picker when clicking on the text 'date'
Alistair
+1  A: 
$(function() {
    $(".click-on-link").click(function() {
        $('#datepicker').datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'dd/mm/yy',
            firstDay: 1,
            onSelect: function(dateText, inst) {
                alert(dateText) // make your AJAX call!
            }
        }).focus(); //make the datepicker appear!
    });
});
aSeptik
it works! thanks! the only thing is that i had to add $('#datepicker').datepicker("destroy"); after the AJAX call in order to close it.
Alex
this is not a "thing", this is the only way! ;-)
aSeptik
true. it also displays it as inline and on the toolbar it doesn't display the close button - also the only way.
Alex