Javascript DOM HTML Select selectedIndex deselect all options

Introduction

Deselect all options:

document.getElementById("mySelect").selectedIndex = "-1";

Click the button to deselect options.

View in separate window

<!DOCTYPE html>
<html>
<body>

<select id="mySelect">
  <option>Apple</option>
  <option>Orange</option>
  <option>Pineapple</option>
  <option>Banana</option>
</select>//  w w w  . jav  a  2s .  com


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

<script>
function myFunction() {
  document.getElementById("mySelect").selectedIndex = "-1";
}
</script>

</body>
</html>



PreviousNext

Related