/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Automatically select current day in dynamic select

Hi all,

I’m only new to javascript, but what I need to do is dynamically create a select, filling it with the days of the month. I also need the select to default to the current day. This is the code I currently have:

function fillDay(){
days = 0;

for(i = 1; i < 32; i++)
{
document.getElementById(‘currDay’).options[i] = new Option(days+i);
}

Then I call that function. Currently it fills a select with all the days of the month (eg 1, 2, 3 etc) and works perfectly. However, I have spent ages trying to figure out how to set the ‘selected’ attribute of the select to be the current day. Any idea on how to do that?

Thanks!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@HetfieldauthorNov 02.2009 — sorry guys, after playing with it a bit more I finally got it. The solution was to put

document.getElementById('currDay').selectedIndex = currDay;

after the for statement, so the solution looks like this:

var d = new Date();

currDay = d.getDate();

function fillDay(){

days = 0;

for(i = 1; i < 32; i++)

{

document.getElementById('currDay').options[i] = new Option(days+i);

}

document.getElementById('currDay').selectedIndex = currDay;

}
×

Success!

Help @Hetfield spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...