Archive: June 29, 2007
I always wind up needing functions to dynamically populate the options on a <select> list, or select a given item in an existing <select>. So I've finally written them out in a reusable form:
<select id="foo" onchange="alert(this.value)"/>
<script language="javascript" type="text/javascript">
function loadSelectOptions(selectId, keys, values, selectedOption){
var list = document.getElementById(selectId)
if(!list)
return;
var selectedIndex = 0;
list.length = 0;
var found = false;
for(var i=0,n=keys.length; i<n; i++){
list.options[i] = new Option(values[i], keys[i]);
if(selectedOption == keys[i]){
list.selectedIndex = i;
found = true;
}
}
return found;
}
function selectSelectOption(selectId, value, runEvent){
var list = document.getElementById(selectId)
if(!list)
return;
var options = list.options;
for(var i=0,n=options.length; i<n; i++){
if(options[i].value == value){
list.selectedIndex = i;
if(runEvent)
list.onchange();
return true;
}
}
return false;
}
loadSelectOptions("foo",['foo','bar','baz'], ['FOO','BAR','BAZ'],'baz')
var list = document.getElementById("foo")
list.remove(1)
selectSelectOption("foo","baz")
</script>
|
Generated in about 0.049s. (Used 7 db queries) |
new⇒Girls, please don't get breast implants
you people implants are fine thesedays but they are like body organsnot e...
kym: Oct 15, 5:25pm