I want to set the value of an ion-datetime input programmatically.
<ion-datetime-button datetime="date"></ion-datetime-button>
<ion-modal>
<ion-datetime label="Date" name="date" type="date" required presentation="date" id="date" tabindex="4"></ion-datetime>
</ion-modal>
When the DOM is ready I run:
function init() {
myForm.elements["date"].value = formatDate(new Date(), "y-M-d");
}
window.addEventListener("load", init)
But this fails with:
Uncaught TypeError: Cannot set properties of undefined (setting ‘value’)
It seems the date
element is not there. Adding a small delay helps:
setTimeout(() => {
myForm.elements["date"].value = formatDate(new Date(), "y-M-d");
}, 1000);
But only sometimes. Occasionally date
is still not there 1s after the DOM is ready.
Is there an equivalent “onload” event for Ionic? Something like ionViewDidEnter
which, unfortunately, is Angular only.
This is VanillaJS and not Angular.
Possibly related: How to tell when Ionic has loaded in plain JS