JavaScript set selected on load
February 13, 2006 /
Filed under: JavaScript, PHP
"Pre-selecting" form valuesHTML select lists, radio buttons, and checkboxes allow you to set an attribute which "pre-selects" a specific value:
... or:
Using these attributes, you can set a "preset" value, for the user. In other words, when the user first views the form, certain values may already be selected for them:
Now, why would anyone want their form values pre-selected? A few reasons, actually:
Example select list: monthsLet’s take an example select list, where the user must select a month: <select id="month"> <option id="january" value="january">January</option> <option id="february" value="february">February</option> <option id="march" value="march">March</option> <option id="april" value="april">April</option> <option id="may" value="may">May</option> <option id="june" value="june">June</option> <option id="july" value="july">July</option> <option id="august" value="august">August</option> <option id="september" value="september">September</option> <option id="october" value="october">October</option> <option id="november" value="november">November</option> <option id="december" value="december">December</option> </select> Highlight the current monthBy default, when the page loads, the first option highlighted, in this select list, will be January, since that’s the first option in the HTML code. But, what if we’d like to save the user a step, and automatically have the current month highlighted? Within our HTML, we could simply apply the There. Done. Entry finished. Go eat some cake. Actually, not quite. What if I don’t want to go into the HTML, at the end of every month, and manually change the More specifically, we can either have a server-side programming language do it - or we could have JavaScript do it. Server-side approachThe server-side approach works fine, but it’s a bit more long-winded than JavaScript. First, we find the current month (I’ll use PHP, for this example):
Next, we create an array with each month as a value: $months_array = array( "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" ); Next, we loop through our array, and write out the entire select list: For the current month of February, this code would result in: This isn’t so bad, I guess - but consider the situation where you’d like to store your select list HTML, and echo it on another part of the page. The above code would not work, in this case, since we echo it immediately. To solve this problem, we could store the above code in a function or class (but I won’t get into that code, now...). I’d rather move on to my "preferred approach" for automatically setting the JavaScript/DOM approachFirst, we find the current month: var months = new Array(12) months[0] = "january"; months[1] = "february"; months[2] = "march"; months[3] = "april"; months[4] = "may"; months[5] = "june"; months[6] = "july"; months[7] = "august"; months[8] = "september"; months[9] = "october"; months[10] = "november"; months[11] = "december"; var date_now = new Date(); var month_now = date_now.getMonth(); Next, we set the select list to a variable, for easy reference:
Next, we grab the
And finally, we simply change that
That’s it. Here is an example of a select list, that always shows the current month: Just make sure you include this code as a result of the page loading initially (onLoad, or whatever you use to capture the load event). Which approach is better?Either approach works the same. I prefer the client-side (JavaScript) approach, because it allows more freedom, but it’s entirely up to you. Comments/Mentions# Chris at 5/23/2007 10:32 pm cst
What if you don't have an ID value for the option you want to select? I can get the Select using getElementById and I know the "value" value that I want to select. Any suggestions? TIA. # Matthom at 5/24/2007 5:45 am cst
Chris, I think the I could be wrong, but I think this is the case. # Bart McLeod at 6/11/2007 7:59 am cst
The current id's don't use MEX CAN or US prefixes in the ID, so there is no way to preset NL province, since it occurs in MEX and CA... (this is, if you want the preset to reflect the entry that was saved as 'NL' by a user). Thanks a lot man, that really helped with implementing some of the code I was working on. =) # Jay at 11/12/2008 9:05 pm cst
Why do you want to create a month array manually when you can let PHP do it for you? } ?> # TheBrain at 1/29/2009 12:09 pm cst
@#7: that's right, PHP can do this for you... I preffer the server side, if a user has javascript disabled it would still be always accurate :) Greetings ;) # Suren Sarathkumara at 12/19/2009 2:28 am cst
You don't have to set value for the options at all.
|
Recent Comments
Recent Music Listens
|

Excellent post, here’s my issue:
On my help page there are several topics from which to choose. If a visitor chooses a topic, he is taken to that page.
I would like to have the default value select when the visitor comes back to that (help) page. Since the page is already loaded, the selected="selected" does not work and his last selection remains present in the drop down. Is there an ’onrefresh’ function in Javascript? (I know, I know, I’ll Google it now); or is there a better way still?