SmartekWorks
SmartekWorks

Reputation: 148

Javascript array access in Internet Explorer

I've a HTML code snippet like follows:

<form>
  <select name="mySelect">
    <option value="1">text_1</option>
    <option value="2">text_2</option>
  </select>
</form>

I found out that in Internet Explorer, Javascript below can work:

document.forms[0].mySelect.options(0).text

But in Firefox or Chrome, it can not work and reports such error message:

document.forms[0].mySelect.options is not a function

In my opinion, mySelect.options should be an array, thus must be accessed by mySelect.options[0]. I also checked the DOM api and found no options function available.

Does it mean in the IE javascript engine, all arrays can be treated as both a collection and a function? Any advice will be appreciated, thanks in advance!

Upvotes: 1

Views: 596

Answers (1)

Teemu
Teemu

Reputation: 23396

Notice, that options is not an array, it's HTMLCollection, which is an array-like object. This is also a host object, hence it can behave differently from JS objects.

In IE you can call HTMLCollection as it was a function (at least in older IEs):

select.options(vIndex [, iSubIndex] );

Here vIndex is either an integer representing an index, or a string referring name property. As you can have multiple similar names, iSubIndex is used to construct a collection of all elements that have a name or id property equal to the string, and then retrieves from this collection the element at the position specified by iSubIndex.

Upvotes: 2

Related Questions