Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Get value of selected option

  • selectedIndex
  • getElementById
  • addEventListener
  • click
<select id="id_of_select">
<option></option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
</select>

<button id="btn">Show selected</button>

<div id="display"></div>

<script src="get_selected_option.js"></script>
"use strict";

function show_selected() {
    var selector = document.getElementById('id_of_select');
    var value = selector[selector.selectedIndex].value;

    document.getElementById('display').innerHTML = value;
}

document.getElementById('btn').addEventListener('click', show_selected);;