accessing html collections

Discussion in 'Javascript' started by mumrah@gmail.com, Dec 7, 2005.

  1. Guest

    When getting different elements out of an array in JS, i know
    array.item(0) and array[0] both return the 0th element. My question,
    will this work in any instance?
     
    , Dec 7, 2005
    #1
    1. Advertising

  2. VK Guest

    wrote:
    > When getting different elements out of an array in JS, i know
    > array.item(0) and array[0] both return the 0th element. My question,
    > will this work in any instance?


    There is not item() method in Array object and myArray.item(0) will
    lead to an error "Object doesn't support this property or method".

    HTMLCollection (or simply Collection) is a far relative of Array but it
    is not Array (neither it's a Hashtable).

    You should read through
    <http://www.geocities.com/schools_ring/ArrayAndHash.html> for a clearer
    picture.
     
    VK, Dec 7, 2005
    #2
    1. Advertising

  3. Randy Webb Guest

    said the following on 12/7/2005 8:35 AM:
    > When getting different elements out of an array in JS, i know
    > array.item(0) and array[0] both return the 0th element. My question,
    > will this work in any instance?


    Only in IE. [] is for Arrays. () is for function calls. IE makes () for
    whatever it wants to use them for. Use [].

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Dec 7, 2005
    #3
  4. Guest

    So if i'm parsing through the DOM, and i have some parents and i access
    it's children:

    object.childNodes

    I should access the children via [0] and not item(0) ?

    example: document.getElementById('foo').childNodes[0] as opposed to
    document.getElementById('foo').childNodes.item(0)

    I had always used the [] to access array elements in JS until i
    starting writing this program that has to parse through an xml dom. The
    site that i read up on that topic used to item() method so, naturally,
    i used it as well. But i'm happy to change back to using brackets. less
    typing :)
     
    , Dec 7, 2005
    #4
  5. VK Guest

    wrote:
    > So if i'm parsing through the DOM, and i have some parents and i access
    > it's children:
    >
    > object.childNodes
    >
    > I should access the children via [0] and not item(0) ?
    >
    > example: document.getElementById('foo').childNodes[0] as opposed to
    > document.getElementById('foo').childNodes.item(0)
    >
    > I had always used the [] to access array elements in JS until i
    > starting writing this program that has to parse through an xml dom. The
    > site that i read up on that topic used to item() method so, naturally,
    > i used it as well. But i'm happy to change back to using brackets. less
    > typing :)


    item() is Microsoft method, maybe someone else accepted it either, but
    the most universal way is collection

    Some IE-specific collections are locked for conventional use: you have
    to wrap them into Microsoft Enumerator object first. These are for
    example file/folder collections returned by FileSystemObject. But in
    regular DOM brackets are more reliable in all curcumstances.
     
    VK, Dec 8, 2005
    #5
  6. VK wrote:

    > wrote:
    >> When getting different elements out of an array in JS, i know
    >> array.item(0) and array[0] both return the 0th element. My question,
    >> will this work in any instance?

    >
    > There is not item() method in Array object and myArray.item(0) will
    > lead to an error "Object doesn't support this property or method".
    >
    > HTMLCollection (or simply Collection) is a far relative of Array but
    > it is not Array (neither it's a Hashtable).


    Whether that is true or not depends on what you consider a Hashtable.

    Elements of HTMLCollections (where there is no Collection interface defined
    or used in W3C DOM Level 2+) can be accessed by alphanumeric indexes, where
    in ECMAScript implementations this feature is realized through standard
    property accessor syntax, as with any other object; the difference is that
    adding or removing one element from the collection results in the
    recalculation of the assigned numeric indexes of each element, and that
    modifying the value of an element accessed through an alphanumeric index
    immediately immediately affects subsequent accesses to that element through
    its numeric index and vice-versa.

    > You should read through
    > <http://www.geocities.com/schools_ring/ArrayAndHash.html> for a clearer
    > picture.


    No, you should not. That document is filled with VK's misconceptions about
    Array objects especially and the used programming languages in general.

    Search this group for "property accessor" and "array" for a clearer picture
    of what really happens.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Dec 8, 2005
    #6
  7. VK wrote:

    > wrote:
    >> So if i'm parsing through the DOM, and i have some parents and i access
    >> it's children:
    >>
    >> object.childNodes
    >>
    >> I should access the children via [0] and not item(0) ?


    It is virtually the same in an HTML 4.01/XHTML 1.0 context.
    Whether or not the former should be used depends on the context.

    >> example: document.getElementById('foo').childNodes[0] as opposed to
    >> document.getElementById('foo').childNodes.item(0)
    >>
    >> I had always used the [] to access array elements in JS until i
    >> starting writing this program that has to parse through an xml dom.


    The XML DOM is not DOM Level 2 _HTML_. The property accessor method is
    defined for that DOM which applies to HTML 4.01 and XHTML 1.0 documents
    only.

    >> The site that i read up on that topic used to item() method so,
    >> naturally, i used it as well. But i'm happy to change back to using
    >> brackets. less typing :)

    >
    > item() is Microsoft method, maybe someone else accepted it either,


    Wrong. item() as used here is a method of DOM Level 2 HTML's HTMLCollection
    interface. Using the property accessor syntax with a numeric parameter is
    the same as calling this method:

    <URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-75708506>
    <URL:http://www.w3.org/TR/DOM-Level-2-HTML/ecmascript-binding.html>

    > but the most universal way is collection


    What is to be noted is that access methods of the W3C DOM tend to be poorly
    implemented [ref. Element::setAttribute()], therefore the property accessor
    syntax /may be/ the more reliable one, the _proper_ context provided (see
    above). It also is less error-prone _there_ because property accessor
    syntax is a feature of the programming language, while a method of DOM
    objects is a feature of the DOM -- the method call fails with a
    ReferenceError if the method is not supported, the property accessor syntax
    will not (unless that expression is used untested as base reference for
    another property access).

    > Some IE-specific collections are locked for conventional use: you have
    > to wrap them into Microsoft Enumerator object first. These are for
    > example file/folder collections returned by FileSystemObject. But in
    > regular DOM brackets are more reliable in all curcumstances.


    You are confusing languages (here: JScript, VBScript) and AOM/DOM. Again.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Dec 8, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitja Semolic

    Access data types in collections by reference

    Mitja Semolic, Jul 22, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    385
    Mitja Semolic
    Jul 22, 2004
  2. Mark Miller
    Replies:
    1
    Views:
    1,086
    Mark Miller
    Jul 18, 2003
  3. Jason

    Control collections

    Jason, Jul 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    373
    William F. Robertson, Jr.
    Jul 28, 2003
  4. Doug Poland
    Replies:
    9
    Views:
    734
    VisionSet
    Sep 27, 2003
  5. mutex
    Replies:
    0
    Views:
    214
    mutex
    Jul 27, 2003
Loading...

Share This Page