Sasanka Panguluri Sasanka Panguluri - 2 months ago 5
jQuery Question

Open Datepicker by clicking a Link

I am using JQuery UI's Datepicker on my web page. Currently, I have been successful in getting it to work with a textfield called "mydate". The code is as follows:

$('#releasedate').datepicker({
changeYear: 'true',
changeMonth:'true',
startDate: '07/16/1989',
firstDay: 1
});


However, I want a small change. Instead of the text field, I would like to display a link called "Select Date" which should open the datepicker, and upon selecting a date, a hidden text field's value needs to be updated.

I tried different ways to do it, but have been unsuccessful. JQuery experts, can you please help me? I am eagerly waiting to receive a solution for this. Thank you very much in advance.

Answer

You can actually just attach the datepicker to an <input type="hidden" /> and make the link a trigger to open it:

$(function() {
    $('#hiddenDate').datepicker({
        changeYear: 'true',
        changeMonth: 'true',
        startDate: '07/16/1989',
        firstDay: 1
    });
    $('#pickDate').click(function (e) {
        $('#hiddenDate').datepicker("show");
        e.preventDefault();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>

<input id="hiddenDate" type="hidden" />
<a href="#" id="pickDate">Select Date</a>

Comments