user3608168
user3608168

Reputation: 111

How to fetch selected dates from jquery datePicker

In my App,we are rendering DatePicker(calender) with the following code.It's coming perfectly.

 $('#date2').DatePicker({
    flat: true,
    date: [],
    current: '2008-07-31',
    format: 'Y-m-d',
    calendars: 1,
    mode: 'multiple',
    starts: 0
});

After I render,it's coming like this.Still Html guy not yet implemented CSS.From the calendar user can select multiple dates.

we are using Backbone and Marionette in Application.What I want If user select any date from calender, needs to fire an event inside the callback function wants to console selected dates().For This I tried with following code but it's not working.

events:{
  "select #date2":"consoleDates"
},
consoleDates:function(event){
 console.log($(event.target).val());
}

Also I tried with change event,This is also not firing.

How can I fix this.

Upvotes: 0

Views: 281

Answers (2)

josephnvu
josephnvu

Reputation: 1242

Ok, if you really want to use this plugin, it won't fire any DOM events. Because this plugin is little dated, I think the concept of CustomEvents haven't been introduced yet. Anyway, you would have to actually bind a custom event in the instantiation of the code. So it should look like so:

$('#date2').DatePicker({
    flat: true,
    date: [],
    current: '2008-07-31',
    format: 'Y-m-d',
    calendars: 1,
    mode: 'multiple',
    starts: 0,
    onChange: function(formatted, dates){
       $('#date2').trigger('datePick', arguments);
    }
});

Then in Backbone it should look like so:

events:{
  "datePick #date2":"consoleDates"
},
consoleDates:function(event, formatted, dates){
  console.log(formatted.join(', '));
}

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82241

Use onselect. Try this:

 $('.selector').datepicker({
   onSelect: function(date) {
       alert(date);
   }
 });

Update: Looking at the doc, the property for this is onChange:

onChange: function(formated, dates){
    console.log(dates);
}

Upvotes: 1

Related Questions