Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery datepicker default date

I am trying to get a attribute of a date picker to pull the date dynamically, however I get uncaught exception errors when I try to set it to a variable.

The errors only occur on pages that do NOT have the calendar (inline). How can I pul the rel tag from the selector without getting this error?

//Event Calendar Home Page and Listing
function calendar_picker () {
$("#calendar-inline").datepicker({
   //defaultDate: $(this).attr('rel'),
    dateformat: 'yy-mm-dd',
    maxDate: '+1y',
    minDate:'-0d',
    hideIfNoPrevNext: true,
    showButtonPanel: false,
    navigationAsDateFormat: false,
    onSelect: function(dateText, inst) {
                 var d = new Date(dateText);
   var fmt1 = $.datepicker.formatDate("yy-mm-dd", d);
   $.ajax({
   type: "POST",
   url: "/events/listing/all/20/",
   dataType: "html",
                        date: "event_date="+fmt1,
   success: function(){
                        window.location.href= "/events/browse/"+fmt1;
    }});}});
}

UPDATE Correct, the commented line is what I am having issues with, What is the correct way to pull the attribute rel from #calendar-inline from inside this. All attempts throw a uncaught error in js

Update 2

function calendar_picker () {
var myDate = new Date($("#calendar-inline").attr('rel'));
    $("#calendar-inline").datepicker({

     dateformat: 'yy-mm-dd',
     defaultDate:myDate,

Solution:

function calendar_picker () {
var myDate = null;
if ($("#calendar-inline").attr('rel') != null) {
     myDate = $.datepicker.parseDate("yy-mm-dd", $("#calendar-inline").attr('rel'));
     }
    $("#calendar-inline").datepicker({

     dateformat: 'yy-mm-dd',
     defaultDate:myDate,
like image 489
matthewb Avatar asked Oct 29 '09 21:10

matthewb


1 Answers

try this:

defaultDate: $("#calendar-inline").attr('rel')

This will attempt to pull the date from the 'rel' attribute and if that doesn't exist it should return null. When null is passed into the datepicker default date it will use today as the default date.

like image 93
David Radcliffe Avatar answered Oct 21 '22 09:10

David Radcliffe