RGS
RGS

Reputation: 4253

Jquery calendar in a div, add date to input on click

I have a jquery ui calendar:

$(function() {
    $("#calendar").datepicker({
        dateFormat: 'dd/mm/yy'
    });
});

and the div calendar to show the calendar:

<div id="calendar"></div>

I also have this input text to get the date into a form:

<input type="text" id="calendario">

<input type="text" id="date">

I'd like to show the calendar in the calendar div and when user pick some date I'd like to add this date in the input (date) field. Any ideas how to do this?

Upvotes: 0

Views: 1131

Answers (1)

gijoe
gijoe

Reputation: 1189

You will need to update your js code with the following:

$(function() {
    $("#calendar").datepicker({
       dateFormat: 'dd/mm/yy',
       onSelect:function(date){
         $('#date').val(date);
       }
    });
});

Datepicker accepts a callback function when user selects a date which you set with onSelect and you can receive as argument the date that the user selected.

Then you query your input field #date and assign the date the value of the input field

Upvotes: 2

Related Questions