/

HTML / CSS / JavaScript Tutorial

HTML Element: <option>

[this page | pdf | back links]

The HTML <option> element indicates an option in a drop-down list.

 

The attributes it can take (other than HTML global attributes and HTML event attributes) include:

 

Attribute

Description

More

disabled

Specified element(s) (here the option-group) to be disabled

Here

label

Title of option group

Here

selected

Indicates that an <option> element should be pre-selected when the page loads

Here

value

Value of element

Here

 

To create or access such an element in JavaScript see here. The corresponding HTML DOM object supports standard DOM properties and methods, and additional properties with the same name and meaning as the attributes of the underlying HTML element referred to above. It also supports the following additional properties:

 

Property

Description

More

defaultSelected

Returns default value of the selected attribute

Here

form

Returns reference to form that contains the option

Here

index

Sets / returns index position of option in drop-down list

Here

text

Sets / returns text of the option

Here

 

The default style applicable to this element is shown here.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
Created using HTML:<br>
<select size="5">
  <optgroup label="Fruit">
    <option value="fruit1">Apple</option>
    <option value="fruit2">Banana</option>
  </optgroup>
  <option value="veg">Vegetables</option>
</select>

<br><br>Created using JavaScript:<br>
<span id="element"></span>

<script>
var x = document.createElement("SELECT")
var x1 = document.createElement("OPTGROUP");
var x1a = document.createElement("OPTION");
var x1b = document.createElement("OPTION");
var x2 = document.createElement("OPTION");
x1a.textContent = "Apple"; x1a.setAttribute("value","fruit1");
x1.appendChild(x1a);
x1b.textContent = "Banana"; x1b.setAttribute("value","fruit2");
x1.appendChild(x1b);
x1.setAttribute("label","Fruit");
x.appendChild(x1);
x2.textContent = "Vegetable"; x2.setAttribute("value","veg");
x.appendChild(x2);
x.setAttribute("size","5");
document.getElementById("element").appendChild(x);
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | HTML Elements


Desktop view | Switch to Mobile