JavaScript Form List Key to Index
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.
JavaScript:
-
function key2index(field,value){
-
for (i=0; i<$(field).options.length; i++){
-
if ($(field).options[i].value == value) return i;
-
} return 0;
-
}
Home | JavaScript | JavaScript Form List Key to Index
You’re currently reading “ JavaScript Form List Key to Index ,” an entry on BRADINO
- Published:
- 3.29.07 / 4pm
- Category:
- JavaScript












