Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically invoke the native datepicker on a mobile device

I want to programmatically kick off the native datepicker on my web page (that is run on iOS and Android). I know that I can use <input type="date" but that would require user input.

How do I kick it off programmatically?

like image 889
AngryHacker Avatar asked Jun 02 '14 14:06

AngryHacker


People also ask

How do you implement a date picker?

If the Controls task pane is not visible, click More Controls on the Insert menu, or press ALT+I, C. Under Insert controls, click Date Picker. In the Date Picker Binding dialog box, select the field in which you want to store the date picker data, and then click OK.

What is Datepicker in JavaScript?

The JavaScript DatePicker (Calendar Picker) is a lightweight and mobile-friendly control that allows end users to enter or select a date value. It has month, year, and decade view options to quickly navigate to the desired date.

Why Datepicker is not working in HTML?

You need to include jQuery, and include it before jQuery UI. You also need to move your code to the end of your document or wrap it in a document ready call. Include a reference to jQuery before jQuery UI. Thanks for the response .


3 Answers

You don't.

You can't tell (/force) the phone to show the date-picker without a user touching a date input.

Which is a good thing, because (at least on iOS) the native datepicker hides the normal browser controls at the bottom of the screen.

like image 63
Cerbrus Avatar answered Oct 13 '22 08:10

Cerbrus


The web platform now ships with the HTMLInputElement showPicker() method, a canonical way to show a browser picker not only for dates, but also time, color, and files. At the time of writing, showPicker() is available in Chrome 99 or later.

Calling showPicker() on an element shows a browser picker to the user. It must be called in response to a user gesture such as a touch gesture or mouse click; otherwise it will fail with a NotAllowedError exception. For security reasons, it will throw a SecurityError exception when it's called in a cross-origin iframe.

A browser picker is shown when the element is one of these types: "date", "month", "week", "time", "datetime-local", "color", or "file".

The example below shows you how to open a browser date picker.

<input type="date">
<button>Show the date picker</button>

<script>
  const button = document.querySelector("button");
  const dateInput = document.querySelector("input");

  button.addEventListener("click", () => {
    try {
      dateInput.showPicker();
      // A date picker is shown.
    } catch (error) {
      // Use external library when this fails.
    }
  });
</script>

A browser picker can also be prepopulated with items from <datalist> or "autocomplete".

See https://developer.chrome.com/blog/show-picker/ and https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/showPicker

like image 4
François Beaufort Avatar answered Oct 13 '22 10:10

François Beaufort


You can,

but it requires any input from the user e.g. touch/click event. From there you can use either yourdateinput.click() or yourdateinput.focus(). The first one is for android, the last one works on iOS.

like image 3
reij Avatar answered Oct 13 '22 08:10

reij