Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html select option selected does not work

Tags:

html

combobox

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

like image 649
Suzan Cioc Avatar asked Jul 15 '12 14:07

Suzan Cioc


People also ask

How do you define selected options in HTML?

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).

How do I make the option selected by default in HTML?

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.

How do you check if a option is selected or not?

isChecked()) alert('this option is selected'); else alert('this is not'); });

How do you display a selected value in a drop-down list?

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.


2 Answers

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>
like image 86
Matzi Avatar answered Oct 18 '22 23:10

Matzi


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.

like image 33
mercurial Avatar answered Oct 18 '22 23:10

mercurial