Forum Moderators: open

Message Too Old, No Replies

strange combo box question

select the correct value

         

iProgram

10:29 am on Sep 28, 2004 (gmt 0)

10+ Year Member



Hello, I want the combo box always selects the first line when the page opens, here is the code:
<select name="test">
<option value="1" selected="selected">Line 1</option>
<option value="2">Line 2</option>
<option value="3">line 3</option>
</select>

No problem at first. But if I select Line 2 and press the refresh button of my browser, the reloaded page still uses line2 as the current selected value, not line 1. What's wrong?

MozMan

9:06 pm on Sep 28, 2004 (gmt 0)

10+ Year Member



I don't think anything is wrong. According to W3C:

selected
When this attribute is present, the option is selected when the document is initially loaded. It is an error for more than one option to be so selected for one from many menus.

Looks like this is appropriate behavior; selected only works on initial load, not reload.

In playing with it in IE, I notice that it will go back to the selected item if I click the "go" button, but not if I click the refresh button, which is right in line with this definition.

-Moz

StupidScript

11:48 pm on Sep 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



iProgram: the code should be:
<select name="test">
<option value="somevalue" selected />Some Item
<option value="someother" />Another Item
</select>

(The "selected" attribute has no value.)

(The only time a select menu is "allowed" to have more than one option selected by default is with a:
<select name="formthing" multiple> multiple attribute.)

You might see different behavior depending on the browser you are using, regardless of the W3C spec, as some browsers reload "hard" and others "soft". A "hard" reload (like typing in the URI and hitting GO) will cause the page to be re-requested from the server, where a "soft" reload (typically, but not always, when one hits the "Reload" button or presses the keyboard equivalent) uses a combination of cached and local session data.

If the browser "remembers" which option was selected, it may indicate that on a "soft" reload. If the browser doesn't maintain that kind of info, it would revert to the default selection indicator(s) in your page code.

photon

12:53 am on Sep 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(The "selected" attribute has no value.)

Not true. Check the W3 specification [w3.org] (under "Boolean attributes").

The attribute is set to "true" by appearing in the element's start tag:

<OPTION selected="selected">
...contents...
</OPTION>

And I believe that the expanded version is required for XHTML strict.