...

DOM getElementsByTagName

brendon99
01-12-2004, 10:04 PM
Hi,

I'm using the getElementsByTagName method to obtain a customer listing of records. So far so good. However, I also want to reference the child nodes of the customer records.

ie.
<customer id="1234" name="Mr Dodd">
<purchase po="1" value="3.00"/>
<purchase po="2" value="4.00"/>
<purchase po="3" value="5.00"/>
</customer>

I used getElementsByTagName("customer") to obtain the list of customers, but I'm having trouble referencing the child nodes ie. purchase.

Does getElementsByTagName("customer") also obtain the child nodes, or only the matching element?

thanks

liorean
01-12-2004, 10:43 PM
It obtains all 'customer' nodes in the document and puts them in a dynamic collection. However, each of those nodes will in turn contain ways to get at just the 'purchase' nodes that belongs to it, either by the childNodes collection if you want all child nodes, or by the method getElementByTagName('purchase'). That is, like this:

document.getElementByTagName('customer').item(0).getElementByTagName('purchase') // => [DOMCollection AllPurchasesByTheFirstCustomer]

[:edit:]Deleted the response, James? Now tell me what it was, I'm curious!

brothercake
01-17-2004, 02:08 AM
I was just wondering whether this is relevant - http://www.codingforums.com/showthread.php?s=&threadid=30656

RadarBob
01-27-2004, 09:53 PM

liorean
01-27-2004, 10:04 PM
Not really. getElementsByName exists for a sole reason: To allow gathering of forms, images, links, frames by the name attribute instead of the id attribute. If you have a closer look at the spec you will find that it is a method of [DOM1HTML] and not CORE, anyway, so it's not present in XML documents.

<http://w3.org/dom/DOMTR>

What function may be useful is getElementsById, however. But if he wanted to use that one I think he would have tried it instead of getElementsByTagName.



Brendon, are you aware that ID may not start with a digit?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum