Date: 2009jul6
Language: javaScript
Q. How do I append an option to an HTML <select> with javaScript?
A. This function does the trick:
function appendSelectOption(objSelect, pretty, value, is_selected) {
if (objSelect == null) return false;
objSelect.options[objSelect.options.length] = new Option(pretty, value, false, is_selected);
return true;
}
Example:
<select id=myselect>
</select>
<script>
var obj = document.getElementById('myselect');
appendSelectOption(obj, 'Yes', 'yes', false);
appendSelectOption(obj, 'No', 'no', false);
appendSelectOption(obj, 'Maybe', 'maybe', true);
</script>
| What this info useful to you? You can donate to say thanks |
Add a comment
Sign in to add a comment