Created
April 6, 2011 00:30
-
-
Save davidcalhoun/904893 to your computer and use it in GitHub Desktop.
refactor of http://www.reddit.com/r/javascript/comments/gjg4u/create_select_option_and_fill_input_text_with/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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