"GetElementsByTagName().childNodes" is undefined

K

Kevin

Hello,

My code (shown below) tries to access the value of an XML element.
After I declare the variable "name" equal to "dog," I then create a
new variable, a, that gets all elements named "dog." Next, my
if...else statement is true for the if statement with this output:

"found name dog with length = 1"

Next, I try to access the first child node of all elements named
"dog." On this line my code fails with an
"xmlDoc.getElementsByTagName(name).childNodes[0]; not defined" error.

var name = "dog";

var a = xmlDoc.getElementsByTagName(name);

if(a) {alert("found name " + name + "with length = " + a.length);}
else {alert(QuoteName + " not found");}

var b = xmlDoc.getElementsByTagName(name).childNodes[0]; //
6-27-2010: undefined error

Please let me know your thoughts.

Thank you,
Kevin
 
M

Martin Honnen

Kevin said:
var b = xmlDoc.getElementsByTagName(name).childNodes[0]; //
6-27-2010: undefined error

getElementsByTagName gives you a NodeList, you can then access items in
the NodeList with the item method e.g.
var el0 = b.item(0);
or with the ECMAScript short cut of bracket notation
var el0 = b[0];
then you should be able to access childNodes of such an item in the
NodeList. Accessing the childNodes of the NodeList itself does is not
something the DOM allows, if you want such features look into XPath or
XQuery there you can do e.g.
//foo/child::node()
to access all child nodes of all foo elements.
 
K

Kevin

Martin, thanks for your help.

Here's what I did -

var a = xmlDoc.getElementsByTagName(name);


if(a) {alert("found name " + name + "with length = " +
a.length);}
else {alert(QuoteName + " not found");}

var b = a.item(0);
alert("b has length = " + b.length + " and value = " +
b.nodeValue);

// output: "b has length = undefined and value = null

Can you please tell me why I'm getting length = undefined and value =
null?

Thanks,
Kevin
 
R

RobG

Martin, thanks for your help.

Here's what I did -

       var a = xmlDoc.getElementsByTagName(name);

The getElementsByTagname method returns a NodeList, so a is now a
NodeList.

        if(a)   {alert("found name " + name + "with length = " +
a.length);}
        else {alert(QuoteName + " not found");}

        var b = a.item(0);

Variable b is assigned a reference to the first member of a, which
should be an ELement (given that a.length > 0), which implements the
Element interface which builds on the Node interface.

Element:
<URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-745549614 >

Node:
        alert("b has length = " + b.length + " and value = " +
b.nodeValue);

// output: "b has length = undefined and value = null

Can you please tell me why I'm getting length = undefined and value =
null?

Because b is an Element and neither interface Element nor Node define
a length property and you haven't assigned a value to one.

I think what you might be trying to do is discover how many child
nodes b has. If that is the case, then:

b.childNodes.length

should do the trick.
 
G

Garrett Smith

[snip links, etc]
Because b is an Element and neither interface Element nor Node define
a length property and you haven't assigned a value to one.

Right, don't expect a length property. Also don't expect to not have a a
length property. Some elements actually do have a length property (such
as FORM, SELECT, ...).
 
R

RobG

[snip links, etc]
Because b is an Element and neither interface Element nor Node define
a length property and you haven't assigned a value to one.

Right, don't expect a length property. Also don't expect to not have a a
length property. Some elements actually do have a length property (such
as FORM, SELECT, ...).

Ah yes, they would be defined as properties of relevant interfaces in
the DOM HTML and HTML5 (draft) specifications. The OP seemed to be
using XML though, so only Core seemed relevant.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top