Search code examples
jqueryhtmldatepickerjquery-ui-datepicker

Need to make each date in jquery-ui-datepicker a custom hyperlink with the date


I am using the jquery datepicker and need to make each day in the calendar link to a page.

So for example if you click the date 12 Jan it will link to http://myurl.com/12-01-17 or for 2 Feb it will link to http://myurl.com/2-02-17.

    jQuery( function() {
    jQuery("#datepicker").datepicker({
                    prevText: "<",
                    nextText: ">",
                    dayNamesMin: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ],
                    dateFormat: 'd MM yy',
                    showOtherMonths: true,
                    selectOtherMonths: true,
    });
});


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

<a href="http://my-url.com/DATEPICKER DATE HERE"></a>

Solution

  • Just use onSelect option for datepicker.

    $(function(){
      $( "#datepicker" ).datepicker({
        dateFormat: "dd-mm-yy",
        onSelect: function (date) {
          window.location="http://my-url.com/"+date.toString();
          //date.toString() is now in dd-mm-yyyy format, change it to meet your requirements
        }
      });
    
    });
    <link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <p>Date: <input id="datepicker" type="text"></p>
    <a id="link" href="http://my-url.com/DATEPICKER DATE HERE">click me</a>