Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript open input date picker event

I am trying to open a date calendar input when a change event is activated.

There is a drop down field, and when a user clicks the "Specific Date" option, I would like the calendar to actually open up, however I have not been successful thus far.

The area I am wanting to open the calendar, I have added the comment: 'Below is what I have tried to open the date picker calendar'.

const datePickerEl = document.getElementById("js_datePickerToggle");

const hideDatePicker = () => {
  datePickerEl.classList.add("displayNone");
};

const getDateRange = (option) => {
  if (option === 'Today') {
    hideDatePicker();
  } else {
    // display datepicker
    datePickerEl.classList.remove("displayNone");

    /*
      * Below is what I have tried to open the date picker calendar
      * datePickerEl.focus();
      * datePickerEl.click();
      * datePickerEl.select();
    */
  }
}

const dateOptionEls = document.getElementsByName("dateRange");

dateOptionEls[0].addEventListener('change', (event) => {
  const optionValue = dateOptionEls[0].options[dateOptionEls[0].selectedIndex].text;
  getDateRange(optionValue);
});
.displayNone {
  display: none;
}
<div>
  <label>Date</label>
  <select name="dateRange" dateRange=''>
    <option value="Today">Today</option>
    <option type="date" value="Specific date">Specific Date
      <input type="date" id="js_datePickerToggle" class="displayNone">
    </option>  
  </select>
</div>

Really appreciate your time.

like image 592
Spangle Avatar asked Apr 13 '26 21:04

Spangle


1 Answers

You can open the picker with inputElement.showPicker() if it's of the type <input type="datetime-local">.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/showPicker

like image 63
GeeDawg Avatar answered Apr 15 '26 12:04

GeeDawg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!