53

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,
    });
});
</script>

It's like it doesn't register the onSelect function. What am I doing wrong?

Linger
  • 14,942
  • 23
  • 52
  • 79
Tommy Jakobsen
  • 2,323
  • 6
  • 39
  • 66

8 Answers8

92

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
  • 5,549
  • 9
  • 34
  • 38
  • 2
    In js you can ommit the semicolon if that's the end of the line and, like in many languages, you can put a comma after the last property. – soger Jul 07 '16 at 12:04
15

datePicker's onSelect equivalent is the dateSelected event.

$(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 the dateSelected event and other events being bound.

Ben Koehler
  • 8,633
  • 3
  • 23
  • 23
8
$('.date-picker').datepicker({
                    autoclose : true,
                    todayHighlight : true,
                    clearBtn: true,
                    format: 'yyyy-mm-dd', 
                    onSelect: function(value, date) { 
                         alert(123);
                    },
                    todayBtn: "linked",
                    startView: 0, maxViewMode: 0,minViewMode:0

                    }).on('changeDate',function(ev){
                    //this is right events ,trust me
                    }
});
Umang Patwa
  • 2,795
  • 3
  • 32
  • 41
Z.Y
  • 81
  • 1
  • 1
5

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
  • 56,243
  • 7
  • 59
  • 69
  • "Error: Object doesn't support this property or method" on line 14, which is: $('.date-pick').datepicker( { – Tommy Jakobsen May 20 '09 at 13:02
  • 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 May 20 '09 at 13:19
4

The best solution is to set the datepicker defaults

folows the code that I used

$.datepicker.setDefaults({
    onSelect: function () {
        $(this).focus();
        $(this).nextAll('input, button, textarea, a').filter(':first').focus();
    }
});
Itamar
  • 59
  • 1
  • 5
3
<script type="text/javascript">
    $(function() {
        $("#datepicker").datepicker({ 
              onSelect: function(value, date) { 
                 window.location = 'day.jsp' ; 
              } 
        });
    });
 </script>

<div id="datepicker"></div>

I think you can try this .It works fine .

Judy
  • 31
  • 1
1

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 -- problem with jquery datepicker onselect . Removed the $("#datepicker").datepicker(); once and it worked.

Community
  • 1
  • 1
srilatha
  • 11
  • 2
  • 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 Dec 23 '09 at 18:41
0

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
  • 524,688
  • 99
  • 697
  • 795
  • 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 May 20 '09 at 12:56