[Javascript] Directly addressible options

Michael Dougherty Michael_Dougherty at PBP.com
Wed Apr 23 12:28:49 CDT 2003


Is there a better way to do this?

<select id='MySelect'>
  <option id='MySelect-AN000135' value='AN000135'>Account 135</option>
  <option id='MySelect-AN000246' value='AN000246'>Account 246</option>
  <option id='MySelect-AN000789' value='AN000789'>Account 789</option>
</select>

Given the value "AN000789", I determine that i want "Account 789" to be the
current/active option for MySelect.

  ref.MySelect-AN000789.selected = (true)

I don't want to iterate every option value compared to my key.  ex:

lcKey = 'AN000789'
for (var lnI=0; lnI<ref.MySelect.length; lnI++) {
  if (ref.MySelect.options[lnI].value == lcKey) {
    ref.MySelect.selectedIndex = lnI
    }
  }

Suppose there were a large number of options in the select, and that this
operation were being peformed multiple times...  an interative solution
degrades performance in direct proportion to the number of items iterated
while a direct call should (in theory) be the same performance regardless of
the number of items.



More information about the Javascript mailing list