[thelist] Help with document.all.item("SomeElement").item(i)

Jeff jeff at lists.evolt.org
Wed Oct 4 13:01:13 CDT 2000


: From: <Gregory.John.Toland at census.gov>
: Help please!!!!  I can not figure this out.  If you run this
: code only odd item numbers are shown by the following
: line:
:  alert ( document.all.item( "txtTopic" ).item( i ).value );

it took me a bit to find it, but the problem is with your logic.

the problem is with removing the item within the for() loop.  let's say you
had only 5 items.  here is how it would look each time through the loop

i = 1;
item[1] = 'item 1';
item[2] = 'item 2';
item[3] = 'item 3';
item[4] = 'item 4';
item[5] = 'item 5';
item[i] = 'item 1';

i = 2;
item[1] = 'item 2';
item[2] = 'item 3';
item[3] = 'item 4';
item[4] = 'item 5';
item[i] = 'item 3';

i = 3;
item[1] = 'item 3';
item[2] = 'item 4';
item[3] = 'item 5';
item[i] = 'item 5';

i = 4;
item[1] = 'item 4';
item[2] = 'item 5';
item[i] = <--- error: item[i] is undefined

so, as you can see it's doing exactly what you're telling it to do.

now, before moving on to fixing the logic to the solution of your problem,
i'd like to make a couple of suggestions with regard to your javascript

first, it's good to see that you're not only specifying the language in the
script tag, but also specifying the type as well.  however, as we get closer
to xhtml support and/or xml support it will become more important to quote
attribute values (i.e. <script language="JavaScript"
type="text/javascript">).  the same goes for all the rest of the html tags
and their attribute values in your work.  might as well get into practice
now eh?

second, when referring to items by their index within an array, the
preferred method (and easier to parse with the eyes as index references) is
to use square brackets ([,]) rather than paranthesis.  for example,


instead of:

alert (document.all.item("txtTopic").item(i).value);

also, to avoid conflicts and the unreadability introduced with massive quote
escaping when working with html within javascript, i recommend using single
quotes in all places with javascript where you would normally use
double-quotes.  for example:


instead of:

alert (document.all.item("txtTopic").item(i).value);

third, and finally, the method you've chosen to interact with the dropdown
menu is not cross-browser.  there is another way of doing it that far more
browsers will be able to use with no lost functionality.  simply access the
dropdown through the forms array, and then the elements array, and then the
individual option you wish to interact with via the options array.  assuming
it's the first form on the page and the dropdown is the second element in
that form and you want to access the value of the 3rd option in that
dropdown, here's how you would do that:


or, by name, using your example:


so, on to your logic problem.  if all you're trying to do is delete all of
the items from the dropdown, then just set it's length to zero.

document.frmResults.txtTopic.options.length = 0;

if you're trying to read, then delete, then make sure that your read and
delete always look at the item in the position that you start incrementing
from.  in your example that would be the index of 1 (since the index of 0 is
not an actual value, but a cue to select).

for (i = 1; i < nLength; i++ )

function deleteOption(index)
  dropDown = document.frmResults.txtTopic;
  for(i = index; i < (dropDown.length - 1); i++)
    dropDown.options[i].text = dropDown.options[i + 1].text;
    dropDown.options[i].value = dropDown.options[i + 1].value;
  dropDown.length = theList.length - 1;

what this will do is cycle through all the options, reporting and removing
the first instance each iteration until it's reached the original length of
the list.

good luck,


mailto:jeff at members.evolt.org

More information about the thelist mailing list