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:
  1. function key2index(field,value){
  2.     for (i=0; i<$(field).options.length; i++){
  3.         if ($(field).options[i].value == value) return i;
  4.     } return 0;
  5. }

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank

Home | JavaScript | JavaScript Form List Key to Index