views:

8172

answers:

8

Hi,

I can't get onSelect working on my jQuery datepicker.

Heres my code:

<script type="text/javascript">
$(function() {
    $('.date-pick').datePicker( {
        onSelect: function(date) {
            alert(date)
        },
        selectWeek: true,
        inline: true,
        startDate: '01/01/2000',
        firstDay: 1,
    });
});

It's like it doesn't register the onSelect function.

Any help will be much appreciated.

Tommy.

A: 

It should be "datepicker", not "datePicker" if you are using the jQuery UI DatePicker plugin. Perhaps, you have a different but similar plugin that doesn't support the select handler.

tvanfosson
Aha. Didn't knew there were different date picker plugins.Whats the correct plugin to use then?What I need is the "selectWeek: true" feature, and onSelect.
Tommy Jakobsen
A: 

A semi colon after alert(date) would more than likely help too :).

kastermester
A: 

The function datepicker is case sensitive and all lowercase. The following however works fine for me:

$(document).ready(function() {
  $('.date-pick').datepicker( {
    onSelect: function(date) {
        alert(date)
    },
    selectWeek: true,
    inline: true,
    startDate: '01/01/2000',
    firstDay: 1,
  });
});
samjudson
"Error: Object doesn't support this property or method" on line 14, which is: $('.date-pick').datepicker( {
Tommy Jakobsen
See comment above about which 'date picker' you are using - my example is the JQuery UI one, yours appears to be a different one.
samjudson
A: 

No comma after the last property.

Semicolon after alert(date);

Case on datepicker (not datePicker)

Check your other uppercase / lowercase for the properties.

$(function() {
    $('.date-pick').datepicker( {
        onSelect: function(date) {
            alert(date);
        },
        selectWeek: true,
        inline: true,
        startDate: '01/01/2000',
        firstDay: 1
    });
});
Antony Carthy
+1  A: 

As far as I know you are using http://www.kelvinluck.com/assets/jquery/datePicker/

Now the latest version is 2.1.2

You can download from here

For me the best datepicker is jQuery UI datepicker

Gordian Yuan
jQuery UI datepicker doesn't support the selectWeek property, does it?
Tommy Jakobsen
What is the role of selectWeek property?
Gordian Yuan
It lets you select (and highlights) a week
Tommy Jakobsen
oh!In this case jQuery UI datepicker can't do that, it only can Select a day in the new version(means 1.7).Perhaps you can modify it to fit your needs.
Gordian Yuan
No idea how to do that as I'm very new to jQuery :) But I can live without it. jQuery UI datepicker is very nice indeed, so I'll use that. Any idea how I can return ISO-8601 week number form a date? Is there such a feature in the date library?
Tommy Jakobsen
Of course!you can using datejs(http://www.datejs.com/).It have getISOWeek() method. Search getISOWeek in http://code.google.com/p/datejs/ page.
Gordian Yuan
+1  A: 

datePicker's onSelect equivalent is the dateSelected event.

<script type="text/javascript">
$(function() {
    $('.date-pick').datePicker( {
        selectWeek: true,
        inline: true,
        startDate: '01/01/2000',
        firstDay: 1,
    }).bind('dateSelected', function(e, selectedDate, $td) {
  alert(selectedDate);
 });
});

This page has a good example showing dateSelected and other events being binded.

Ben Koehler
A: 

I have downloaded the datepicker from jqueryui.com/download and I got 1.7.2 version but still onSelect function didn't work. Here is what i had -

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

$("#datepicker").datepicker({ 
      onSelect: function(value, date) { 
         alert('The chosen date is ' + value); 
      } 
});

I found the solution in this page -- http://stackoverflow.com/questions/1274347/problem-with-jquery-datepicker-onselect . Removed the $("#datepicker").datepicker(); once and it worked.

srilatha
Howdy found the answer in another thread -- http://stackoverflow.com/questions/1274347/problem-with-jquery-datepicker-onselect I was using datepicker event twice on a div. Should only be initialized once.
srilatha
+1  A: 

The initialization of datepicker() should happen only once in your code. For example, you have created two instances for the datepicker() then always whatever you have written in the first instance should work. It just ignores the second or the next initializations. So, make sure you have created instance only once and written everything inside it.

Hope this helps.

Rare Solutions