How to set default selected element in combobox? Neither of the following worked for me:
Version 3:
<select id="parent">
<option value='null'>(Root)</option>
<option selected='yes' value='/'>Main</option>
</select>
Version 2:
<select id="parent">
<option value='null'>(Root)</option>
<option selected='true' value='/'>Main</option>
</select>
Version 1:
<select id="parent">
<option value='null'>(Root)</option>
<option selected value='/'>Main</option>
</select>
In all cases first option is selected on page, not that one which is marked selected.
SOME OTHER DETAILS
(1)
Page source beginning follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...
(2)
Browser is Firefox 13.0.1
Definition and UsageThe <select> element is used to create a drop-down list. The <select> element is most often used in a form, to collect user input. The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the drop-down list will be submitted).
The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.
isChecked()) alert('this option is selected'); else alert('this is not'); });
Method 1: Using the value property: The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.
Add selected
to the option you want to be selected by default.
Check the following example taken from here.
<!-- The second value will be selected initially -->
<select name="choice">
<option value="first">First Value</option>
<option value="second" selected>Second Value</option>
<option value="third">Third Value</option>
</select>
I'm just going to throw this out there because it has driven me crazy on more than one occasion:
When you refresh (F5) a page, Firefox preserves the selected values of any <select> elements on the page.
So if you're like me and write the select menu, load the page to make sure it works, then add the "selected" attribute, and refresh the page to make sure the selected one appears ... it won't (in Firefox, anyway).
Reloading the page entirely by clicking in the Address field and pressing Enter does honor the "selected" attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With