Set dropdown option same as today's date

I have a dataset where the value connected to dropdown4 is a number value, which has options from 1-31 (to filter by date). I want dropdown4 to automatically show the option same as today’s date. For example if today is March 16, then the dropdown automatically change to choice “16” when it’s 00:00 March 16, later shift to “17” when it’s 00:00 March 17.

Have been browsing for similar posts but seemingly no one posted about this. Thank you in advance!

1 Like

Hey Huong, nice request.

To do this, you can use the main Date class in JavaScript to get the day value of today’s date for the visitor, and use the getDate() method to select from the dropdown.

It could look something like this:

$w.onReady(function(){
    //The rest of your code...
    $w("#dropdown4").value = ""+(new Date()).getDate(); //The ' ""+ ' will convert the number to a string.
});

By putting it in the page’s on ready, it will set it as soon as the page loads, however, you can change where that line of code appears to control that design.

One thing to note, you need to make sure that the values match the labels in the dropdown.

It worked, many thanks !!

1 Like