Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

vanillajs-datepicker does not set input value, no change event triggered on date click

I’m using this datepicker and I like it but its not triggering the change event on an input after I click a date.

Here is my code
I tested this on jsFiddle

const elem = document.querySelector('input[name="foo"]');
const datepicker = new Datepicker(elem, {
  autohide: true,
  todayHighlight: true,
  format: "yyyy-mm-dd",
});

elem.addEventListener("change", function(e) {
  console.log(e.target.value);
});
<input type="text" name="foo">

When I click the specific date on the datepicker the change event is not triggered.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need help with this.

I’m also using it with livewire and alpine but after clicking the date the change even is not triggered. here is my laravel code
code

When I tested pickaday it works.

How can I work around this or make it to trigger the change even on vanillajs datepicker.

Thanks in advance.

>Solution :

Your library won’t trigger a native event like onchange which is why is not working. You have to use the library custom event instead

Info in the documentation
changedate

const elem = document.querySelector('input[name="foo"]');
const datepicker = new Datepicker(elem, {
  autohide: true,
  todayHighlight: true,
  format: "yyyy-mm-dd",
});

elem.addEventListener("changeDate", function (e) {
  console.log(e.target.value);
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vanillajs-datepicker@1.3.4/dist/css/datepicker.min.css">
<script src="https://cdn.jsdelivr.net/npm/vanillajs-datepicker@1.3.4/dist/js/datepicker-full.min.js"></script>
<input type="text" name="foo">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading