Option selected Property - Find out if the "banana" option in a drop-down list is selected or not: - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Option

Description

Option selected Property - Find out if the "banana" option in a drop-down list is selected or not:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form>
Select your favorite fruit:
<select>
  <option id="a">Apple</option>
  <option id="orange">Orange</option>
  <option id="pineapple" selected>Pineapple</option> // Pre-selected
  <option id="banana">Banana</option>
</select>/* w  w  w  .j  a va 2  s . com*/
</form>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {
    var v = document.getElementById("orange").selected;
    console.log(v);
}
</script>

</body>
</html>

Related Tutorials