I have a date picker on my page which I initialize with today's date. Before porting to Vue I used:
document.getElementById("entryDate").valueAsDate = new Date()
Courtesy of https://stackoverflow.com/a/13322910/2672848
But this doesn't work for me in Vue. Obviously I need to access the element itself to get the valueAsDate
property, so I do:
const entryDate = ref(null)
const entryDateElem = ref(null)
onMounted(() => {
entryDateElem.value.valueAsDate = new Date()
})
and in my template:
<input type="date" ref="entryDateElem" v-model="entryDate"></input>
but the date picker value is unchanged. If I use the alternate answer in that same Stack Overflow post (setting the value directly) it does work, as expected.
onMounted(() => {
entryDate.value = new Date().toISOString().substr(0, 10)
})
But why doesn't valueAsDate
work? I have checked that entryDateElem.value refers to the element in question.