I have some code that works in Preview mode but not on my published site.
I have two date picker elements on my page, called #dateStart and #dateEnd to allow the user to specify the range of dates for an action. By default, they are preloaded with today’s date.
I have an onReady function on my page. It includes the following:
let yr = new Date().getUTCFullYear();
let mon = new Date().getMonth();
$w('#dateStart').value = new Date(yr, mon - 1, 1);
$w('#dateEnd').value = new Date(yr, mon, 1);
The idea is that the default start and end date are set to the first of the previous month and the first of the current month.
When the page is loaded, the default dates are displayed correctly, both in preview mode and on my published site.
I have a JS function on my page that uses $w(‘#dateStart’).value and $w(‘#dateEnd’).value.
When in preview mode, it always works as expected.
On my published site, when I reference those two date elements I get the current date, not the date that was set in my onReady function. But, if I manually set the dates, my function works correctly with the entered dates.
I suspect this is some kind of race condition that has to do with when the page is loaded, when the elements are loaded, when the onReady is called, etc. But I don’t really have a good idea how to work around this. Any thoughts?