Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the selected date using vanilla js #17

Open
jokerlovescaptainmarvel opened this issue Jul 24, 2023 · 3 comments
Open

How to get the selected date using vanilla js #17

jokerlovescaptainmarvel opened this issue Jul 24, 2023 · 3 comments

Comments

@jokerlovescaptainmarvel

Hello, I'm trying to find a way how to get the selected date from the calendar using vanilla js.

I'm using an inline script but unfortunately, I can't find a way to get the selected date from the calendar.

<script src="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.7.0/datepicker.min.js"></script>

<div inline-datepicker data-date="02/25/2022"></div>

Any help would be appreciated!

@jokerlovescaptainmarvel
Copy link
Author

Any help?

@allestaire
Copy link

allestaire commented Jan 16, 2024

Dude, there documentation is not good,
Which is we are on the same boat.
They dont have an event for onSelect, it must be with the input

I guess they did this on purpose, in order to make us do upgrade.

It seems like there is a method for the Javascript instance

if you instanciate the picket with javascript instead of element
you will get the data

const dp = new DatePicket(element, {})
dp.getDate()

@allestaire
Copy link

One way to listen to value changes is to exntend its class and add your functionality
This code demonstrate on how to trigger onSelect option. so that everytime a new date is selected, it will be trigger

import Datepicker from 'flowbite-datepicker/Datepicker'

class FlowbiteDatepicker extends Datepicker {
  constructor(element, options = {}, rangepicker = undefined) {
    super(element, options, rangepicker)
    this.options = options
  }

  setDate(...args) {
    super.setDate(args);
    if (this.options.hasOwnProperty('onSelect')) {
      this.options.onSelect(this)
    }
  }
}

export default FlowbiteDatepicker

// Usage
new FlowbiteDatepicker(element, {
   onSelect(instant) {
      // Triggered when date is selected
      console.log(instant)
   }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants