[Javascript] Directly addressible options

Chris Tifer christ at saeweb.com
Wed Apr 23 12:43:21 CDT 2003


If I were to go this route (and I probably wouldn't just because
you're relying on the browser handling ID's properly, I'd do something
more like this simply becuase it conforms a little more what it seems
standards might eventually settle at. Specifically, using getElementById
to reference a particular element.


==========================
<form >
<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>

</form>

<script language="javascript" type="text/javascript">
<!--
 var objMySelEl = null
 function selectSomething(strToSelect){
  objMySelEl = document.getElementById(strToSelect)
  objMySelEl.selected = true
 }

 selectSomething("MySelect-AN000246")
//-->
</script>

==========================

Hope that helps,

Chris Tifer
http://www.emailajoke.com

----- Original Message -----
From: "Michael Dougherty" <Michael_Dougherty at PBP.com>
To: <javascript at LaTech.edu>
Sent: Wednesday, April 23, 2003 1:28 PM
Subject: [Javascript] Directly addressible options


> 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.
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript



More information about the Javascript mailing list