Skip to content

Instantly share code, notes, and snippets.

@davidcalhoun
Created April 6, 2011 00:30
Show Gist options
  • Save davidcalhoun/904893 to your computer and use it in GitHub Desktop.
Save davidcalhoun/904893 to your computer and use it in GitHub Desktop.
<!doctype html> <!-- don't forget your doctype! -->
<html>
<head>
<style>
input { display: block; } /* use this instead of <br/> tags in your HTML */
</style>
</head>
<body>
<form name="test" id="test"> <!-- add id so you can select it with DOM2 selector getElementById -->
<select name="somename">
<option value="0">1</option>
<option value="1">2</option>
</select>
<input name="fill" id="fill"> <!-- again, add an id here to make it easier to select -->
</form>
<!-- move scripts to the bottom, so they don't block -->
<script> <!-- type="text/javascript" not needed in HTML5 -->
window.onload = function(){ // make sure to do DOM manipulations after onload or onDOMReady
var options = document.getElementById('test'),
fill = document.getElementById('fill'),
someData = ['One', 'Two'];
options.addEventListener('change', function(e){ /* use options.onchange instead if you want better support (i.e. IE) */
fill.value = 'Option ' + someData[e.target.value];
}, false);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment