473,802 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

accessing html collections

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 '05 #1
6 2382
mu****@gmail.co m 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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 7 '05 #2
VK

mu****@gmail.co m 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.c om/schools_ring/ArrayAndHash.ht ml> for a clearer
picture.

Dec 7 '05 #3
So if i'm parsing through the DOM, and i have some parents and i access
it's children:

object.childNod es

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

example: document.getEle mentById('foo') .childNodes[0] as opposed to
document.getEle mentById('foo') .childNodes.ite m(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 '05 #4
VK

mu****@gmail.co m wrote:
So if i'm parsing through the DOM, and i have some parents and i access
it's children:

object.childNod es

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

example: document.getEle mentById('foo') .childNodes[0] as opposed to
document.getEle mentById('foo') .childNodes.ite m(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[i]

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 FileSystemObjec t. But in
regular DOM brackets are more reliable in all curcumstances.

Dec 8 '05 #5
VK wrote:
mu****@gmail.co m 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.c om/schools_ring/ArrayAndHash.ht ml> 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
Dec 8 '05 #6
VK wrote:
mu****@gmail.co m wrote:
So if i'm parsing through the DOM, and i have some parents and i access
it's children:

object.childNod es

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.getEle mentById('foo') .childNodes[0] as opposed to
document.getEle mentById('foo') .childNodes.ite m(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[i]
What is to be noted is that access methods of the W3C DOM tend to be poorly
implemented [ref. Element::setAtt ribute()], 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 FileSystemObjec t. But in
regular DOM brackets are more reliable in all curcumstances.


You are confusing languages (here: JScript, VBScript) and AOM/DOM. Again.
PointedEars
Dec 8 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7103
by: David D. | last post by:
I want to be able to access cells within an HTML table, via computed row number and column number. I was considering using oTableID.children and oTR.children arrays. My question is what browsers and browser versions support this? If the children constructs are not fairly universally supported, then what is a better approach to doing this?
15
2102
by: Christopher Benson-Manica | last post by:
When are named elements written with script accessible to script? <html><head><script type="text/javascript"> function ready() { alert( document.getElementsByName("div").length ); } </script></head> <body onload="ready()"> <script type="text/javascript"> document.open();
6
2748
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is called "form1", and I have selects called "PORTA", "PORTB" ... etc...
2
5474
by: George W. | last post by:
In perl I'm used to using hashtables of arrays and accessing a particular element like this: $HoA{flintstones} Simple as that. How do I access and update values of arrays stored in my hashtable within C#? I've looked all over the place and I don't see any provision for doing this. I'd rather not have to write a bunch of code to do something that is trivial in other languages.
3
3348
by: AdamM | last post by:
Hi all, When I run my VbScript, I get the error: "ActiveX component can't create object: 'getobject'. Error 800A01AD". Any ideas what I did wrong? Here's my VBScript: dim o set o=getobject(,"ConsoleApplication2.Program") msgbox o.TestString
6
13029
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this is not an ASP.NET related question, but I know this group is knowledgeable and quick with responses. Thanks
7
5679
by: hummh | last post by:
Hello out there, I´m making my first steps with ASP.NET 2.0 and have he following problem: I´ve implemented a Web User Control that sits in the root of my ASP.NET Website. I want to use the Type of the control in a class that´s under the App_code folder. As with ASP.NET 1.x, I tried to reference the Control via the using keyword - but that didn´ t work, because the Web User Control has no namespace (VS.NET didn´t add one).
5
2769
by: Siva | last post by:
Hello I have a dropdownlist inside the gridview as a template column defined as follows: <asp:TemplateField HeaderText="Choose Location"> <ItemTemplate> <asp:DropDownList ID="ddlChooseLoc" runat="server"> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> I have the gridview inside of a master page- content hierarchy.
2
2066
by: newscorrespondent | last post by:
I have a list declared: public System.Collections.Generic.SortedList<int, System.Collections.Generic.List<MTGTracer ActiveList = new SortedList<int,List<MTGTracer>>(); The key is an integer and the value is a list of MTGTracer. The documentation states:
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10304
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.