Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] Directly addressible options


  • From: Michael_Dougherty at PBP.com (Michael Dougherty)
  • Subject: [Javascript] Directly addressible options
  • Date: Wed Apr 23 12:29:57 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.