Select options Collection - Add a "Kiwi" option at index position "1" in a drop-down list: - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Select

Description

Select options Collection - Add a "Kiwi" option at index position "1" in a drop-down list:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form>
  <select id="mySelect" size="8">
    <option>Apple</option>
    <option>Pear</option>
    <option>Banana</option>
    <option>Orange</option>
  </select>
</form>//from  w  ww  .  j  a  v  a 2 s .  c o  m

<button type="button" onclick="myFunction()">Insert option</button>

<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    var c = document.createElement("option");
    c.text = "Kiwi";
    x.options.add(c, 1);
}
</script>

</body>
</html>

Related Tutorials