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
|
Editor Picks
Email NewsletterSubscribe to the digest newsletter to receive posts by email: Recent Comments
Advertisements
|

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?