0

So I am using the air-datepicker (http://t1m0n.name/air-datepicker/docs/) to select a month, presetting to 01/03/2018 for example, which fills a hidden field in my input called startdate2 using the altmethod.

What I want is to update enddate2 with startdate2 + 2 months. I've tried using onChange and a few various methods but nothing seems to be working.

<input type="hidden" name="startdate" id="startdate2" value="" class="inputdate" onChange="getDateAhead()" >
<input type="hidden" name="enddate" id="enddate2" value="" class="outputdate" >

<script>
function getDateAhead()
{
    var start = document.getElementById('startdate2').value;
    var end = start.setMonth(start.getMonth()+2);
    document.getElementById('enddate2').value = end;
}
</script>

Just can't seem to get it working??

  • the `input:hidden` does not trigger the `change` event you have to call it manually or add a [MutationObserver](https://stackoverflow.com/a/31719339/2417602). – vikscool Aug 17 '18 at 10:41

1 Answers1

1

Please check documentation(http://t1m0n.name/air-datepicker/docs/) for the datepicker you are using. Your concern function is onSelect.

var eventDates = [1, 10, 12, 22],
    $picker = $('#custom-cells'),
    $content = $('#custom-cells-events'),
    sentences = [ … ];

$picker.datepicker({
    language: 'en',
    onRenderCell: function (date, cellType) {
        var currentDate = date.getDate();
        // Add extra element, if `eventDates` contains `currentDate`
        if (cellType == 'day' && eventDates.indexOf(currentDate) != -1) {
            return {
                html: currentDate + '<span class="dp-note"></span>'
            }
        }
    },
    onSelect: function onSelect(fd, date) {
        var title = '', content = ''
        // If date with event is selected, show it
        if (date && eventDates.indexOf(date.getDate()) != -1) {
            title = fd;
            content = sentences[Math.floor(Math.random() * eventDates.length)];
        }
        $('strong', $content).html(title)
        $('p', $content).html(content)
    }
})
Charu Maheshwari
  • 1,583
  • 1
  • 8
  • 9