[thelist] Javascript/Dropdown box detection

Jeff Howden jeff at jeffhowden.com
Tue Sep 9 23:17:47 CDT 2003


tom,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Tom Dell'Aringa
>
> >    if(form.acct.options[form.acct.selectedIndex].text
>
> This is not the correct way to get the text value, nor
> is it a particularly good way to address the objects.
> The best way is to use the form and elements array,
> which are DOM lvl 0 compliant and work flawlessly.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

actually, the method being used is shorthand for dom 0.  each form field
becomes a "property" of the form object.  that's what makes accessing fields
named "name" so difficult.  the name property of the form object takes
precedence.  in this instance, there's nothing particularly wrong with the
way the objects and properties are being accessed, saved for the obvious
error of omitting the "[]" bits of the form field name (which will require
the use of the elements array to access them as "[]" have a special meaning
in javascript).

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> First I would set my drop as a variable.
>
>    var myDrop = document.forms['multi'].elements['acct[]'];
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

except that a reference to the form is being passed as "form" so the above
reference should be shortened:

var oDrop = form.elements['acct[]'];

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> Shortened by passing the form on your function call:
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

already being done.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> Secondly, your reference to the text is slightly off:
>
>    if(myDrop[myDrop.selectedIndex].text == "")..
>
> You do not refer to the options when you are grabbing
> the text.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

on one hand you say to use the elements array, but on another you say to not
use the options array?  make up your mind.  ;p

fwiw, the "correct" way is to use the options array, but only because it's
more readable and self-describing.

.jeff

------------------------------------------------------
Jeff Howden - Web Application Specialist
Resume - http://jeffhowden.com/about/resume/
Code Library - http://evolt.jeffhowden.com/jeff/code/




More information about the thelist mailing list