Easy Tutorial
❮ Jsref Obj Regexp Prop Audio Defaultmuted ❯

selected Attribute


Definition and Usage

The selected attribute sets or returns the value of the selected attribute of an option.

This attribute sets the current state of the option. If true, the option is selected. The initial value of this attribute comes from the selected attribute of the <option>.

Syntax

Set selected attribute:

Return selected attribute:


Browser Support

All major browsers support the selected attribute.


Example

This example changes the selected option in the dropdown list:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
<script>
function displayResult(){
    document.getElementById("orange").selected=true;
}
</script>
</head>
<body>

<form>
Select your favorite fruit:
<select>
    <option id="apple">Apple</option>
    <option id="orange">Orange</option>
    <option id="pineapple" selected="selected">Pineapple</option>
    <option id="banana">Banana</option>
</select>
</form>

<button type="button" onclick="displayResult()">Select Orange</button>

</body>
</html>

❮ Jsref Obj Regexp Prop Audio Defaultmuted ❯