views:

3857

answers:

5

I want to have the JQuery Datepicker open when the user clicks on an image. There is no input field where the selected date appears afterwards; I'm just going to save the entered date to the server via Ajax.

Currently I have this code:

<img src='someimage.gif' id="datepicker" />

$(document).ready(function() {
    $("#datepicker").datepicker({
            changeMonth: true,
            changeYear: true,
        }).click(function() { $(this).datepicker('show'); });
 });

But nothing happens when I click on the image. I have also tried saving the result of the datepicker() call to a global var and then calling datepicker('show') from the onclick() event of the image, but same result.

+3  A: 

The jQuery documentation says that the datePicker needs to be attached to a SPAN or a DIV when it is not associated with an input box. You could do something like this:

<img src='someimage.gif' id="datepickerImage" />
<div id="datepicker"></div>

<script type="text/javascript">
 $(document).ready(function() {
    $("#datepicker").datepicker({
            changeMonth: true,
            changeYear: true,
    })
    .hide()
    .click(function() {
      $(this).hide();
    });

    $("#datepickerImage").click(function() {
       $("#datepicker").show(); 
    });
 });
</script>
Jose Basilio
This works, but strangely I lose some functionality with this monthly. For example, I don't get the scroll open animation effect. More importantly, the calendar doesn't go away when I click outside it.
ep4169
Also, what is the purpose of adding that click() method to #datepicker?And by the way, I meant "method", not "monthly". Got calendars on the brain!
ep4169
The reason for adding the click() method to the #datepicker is to make it hide, otherwise it will remain visible. Keep in mind that the standard behavior only works when the calendar is associated with a textbox. You could probably add an .blur() handler to make it hide when you move your mouse away from the calendar.
Jose Basilio
+11  A: 

Turns out that a simple hidden input field does the job:

<input type="hidden" id="dp" />

And then use the buttonImage attribute for your image, like normal:

    $("#dp").datepicker({
        buttonImage: '../images/icon_star.gif',
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,
        showOn: 'both',
     });

Initially I tried a text input field and then set a display:none style on it, but that caused the calendar to emerge from the top of the browser, rather than from where the user clicked. But the hidden field works as desired.

ep4169
Found your own answer and accepted it. +1
Jose Basilio
A: 

HTML: <div class="CalendarBlock"> < input type="hidden"> </div>

CODE: $CalendarBlock=$('.CalendarBlock'); $CalendarBlock.click(function(){ var $datepicker=$(this).find('input'); datepicker.datepicker({dateFormat: 'mm/dd/yy'}); $datepicker.focus(); });

Rahen Rangan
A: 

Hi ,

I am also looking for the same functioanlity with datepicker. I tried both solutions given in this post, but none of those worked for me. can you plese let me know if this solutions worked for you if not did you find any other solution?

Thanks

sowji250
+1  A: 

If you are using an input field and an icon (like this example):

<input name="hasta" id="Hasta"  type="text" readonly  />
<a href="#" id="Hasta_icono" ></a>

You can attach the datepicker to your icon (in my case inside the A tag via CSS) like this:

$("#Hasta").datepicker();

$("#Hasta_icono").click(function() { $("#Hasta").datepicker( "show" ); });

Pablius