JavaScript Form List Key to Index
Like this blog? Consider exploring one of our sponsored banner ads...
JavaScript references list menu (dropdown) elements by their index number, not by the list menu’s key. So if you want to preselect a list menu using JavaScript and you know they key in the key/value pairs that comprise the listmenu, here is a simple function I wrote which you pass the field id and the key you want the index for.
[js]
function key2index(field,value){
for (i=0; i<$(field).options.length; i++){
if ($(field).options[i].value == value) return i;
} return 0;
}
[/js]
About this entry
You’re currently reading “JavaScript Form List Key to Index,” an entry on BRADINO
- Published:
- 3.29.07 / 4pm
- Category:
- JavaScript
- Tags:
No comments
Jump to comment form | comments rss [?]