[Javascript] Directly addressible options

Michael Dougherty Michael_Dougherty at PBP.com
Wed Apr 23 13:56:27 CDT 2003


Why wouldn't one expect the browser to handle ID's properly?  That sounds
like you'd say, "You could keep investments in a BANK, if you expect them to
handle your money properly"

I understand that everyone has a preferred method of accessing objects and
their properties.  Since we're developing an intranet for IE5.5 (and now
IE6) we can tolerate MS's noncompliance.

perhaps it would have been more clear to use pseudocode with no resemblance
to javascript.  I was wondering if there was another strategy for setting an
option to be selected when yoy know the option value, but not the index.
This method (including the more standards compliant rewrite) seems to work
well enough...

(I read over my original, and i admit that "Better" is both subjective and
ambiguous)

-----Original Message-----
From: javascript at LaTech.edu [mailto:javascript at LaTech.edu]
Sent: Wednesday, April 23, 2003 1:43 PM
To: javascript at latech.edu
Subject: Re: [Javascript] Directly addressible options


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

_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript



More information about the Javascript mailing list