Dave's Brain

Browse - programming tips - javascript append html select option

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
Copyright © 2008-2010, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.