var input = document.createElement("input");
input.type = "datetime-local";
input.onchange =
function()
{
console.log("date set");
}
input.showPicker();
The code above will show a Date/Time picker.
Upon selecting a date, the onchange even is fired, as expected.
However, if the user simply clicks outside the picker, to cancel,
what event – if any – gets fired?
onblur doesn’t seem to work.
So far the only very ugly solution I could find,
is to create a full window, top-layer invisible div,
and capture the "exit click" through that.
2
Answers
Answers were right that the picker could not be shown unless a user action triggered it: I was testing from the console only, and from the console apparently this constraint doesn't apply.
The element also needs to be appended to the DOM.
focus(), however, will not work if the element has any of these characteristics:
Finally I had to resort to setting the input's height and width to 0, but also opacity to 0, because a few pixels would still show.
https://www.number137.com/lab/picker/
Set
onblur
listener, and immediately after you callshowPicker
, set it to be focused as well, so that the blur triggers (which is why it doesn’t work now, because there was no focus event, when picker gets shown programmatically).(Note: to trigger native datepicker programmatically, it requires a user action)
Try this: