dropdown list javascript code example

Example 1: dropdown list using js

<!DOCTYPE html>
<html>
<body>

<form>
  Select your favorite browser:
  <select id="myList" onchange="myFunction()">
    <option></option>
    <option>Google Chrome</option>
    <option>Firefox</option>  
    <option>Internet Explorer</option>
    <option>Safari</option>
    <option>Opera</option>
  </select>
<p>Your favorite browser is: <input type="text" id="demo" size="20"></p>
</form>
<script>
function myFunction() {
  var mylist = document.getElementById("myList");
  document.getElementById("demo").value = mylist.options[mylist.selectedIndex].text;
}
</script>
</body>
</html>

Example 2: dropdown list using js

var mylist = document.getElementById("myList");
document.getElementById("favorite").value = mylist.options[mylist.selectedIndex].text;

Tags:

Html Example