Parti
Parti

Reputation: 49

Vanilla JS Datepicker

I'm trying to get Vanilla JS Datepicker to work : https://github.com/mymth/vanillajs-datepicker

It's throwing an error: TypeError: undefined is not an object on e.dataset.date That's an error down in the date picker code, so I'm not sure what's wrong. Here's my callout in a function:

  function setDate() {
    console.log('DatePickerCk: ' , Datepicker);  // shows as 'class ue' - that doesn't seem right
    var cdate = $(mycell).attr('dc-oldval');
    console.log('CurDate: ' + cdate); // ok - 02/07/2020
    var dpelem = $(mycell).find('input');
    $(dpelem).val(cdate);
    console.log('ValofInput: ' , $(dpelem).val()); // ok - "02/07/2020"
    console.log('DpElem: ' , dpelem); // ok - hovering over it in console highlights the element in the DOM
    var datepicker = new Datepicker(dpelem, {
      autohide: true,
      daysOfWeekDisabled: [],
      daysOfWeekHighlighted: [],
      defaultViewDate: cdate,
      nextArrow: '>>',
      prevArrow: '<<',
      todayBtn: false,
      todayBtnMode: 1,
      todayHighlight: true
    });
    Datepicker.locales.en = {
      days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
      daysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
      daysMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
      months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
      monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
      today: 'Today',
      clear: 'Clear',
      titleFormat: 'MM y',
      format: 'dd/mm/yyyy',
      weekstart: 0
    }
  }
  setDate();

I see the date in the input, but the dropdown for the datepicker doesn't show.

Did I not call it correctly? I'm not sure what the problem is.

Upvotes: 2

Views: 5397

Answers (1)

kman
kman

Reputation: 2257

This is a shot in the dark, but what happens if you add [0] to your dpelem when setting up your Datepicker? It looks like you're getting a jquery element vs a DOM element like it's probably expecting.

var datepicker = new Datepicker(dpelem[0]....

I don't really see anything wrong with your code otherwise. I threw together a quick little jsfiddle and it seems fine assuming you've got a valid reference to your input element.

Upvotes: 1

Related Questions