You can make use of the onchange attribute of the <select> element to execute some Javascript code on every change of the dropdown.
<select onchange="myFunction(this)">
(you see that I passed the dropdown element itself as method argument, this is just done for convenience)
You can use the element.options to get all options and element.selectedIndex to get the index of the currently selected option.
function myFunction(dropdown) { var selectedOption = dropdown.options[dropdown.selectedIndex].value; }
You can use document.getElementById() to retrieve any element by id. Imagine that you've the following input elements:
<input type="text" id="foo"> <input type="text" id="bar">
then you can get them as follows:
var foo = document.getElementById('foo'); var bar = document.getElementById('bar');
You can use the element.style.display to change the CSS display property. A value of block means show and a value of none means hide. Now do the math:
function myFunction(dropdown) { var selectedOption = dropdown.options[dropdown.selectedIndex].value; var foo = document.getElementById('foo'); var bar = document.getElementById('bar'); if (selectedOption == 'spoon') { foo.style.display = 'none'; // Hide foo. bar.style.display = 'block'; // Show bar. } else { foo.style.display = 'block'; // Show foo. bar.style.display = 'none'; // Hide bar. } }
To learn more about Javascript and HTML DOM, I recommend this tutorial.