HighDots Forums  

"GetElementsByTagName().childNodes" is undefined

Javascript JavaScript language (comp.lang.javascript)


Discuss "GetElementsByTagName().childNodes" is undefined in the Javascript forum.

Reply
 
Thread Tools Display Modes
  #1  
Old   
Kevin
 
Posts: n/a

Default "GetElementsByTagName().childNodes" is undefined - 07-07-2010 , 09:08 AM






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


Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: "GetElementsByTagName().childNodes" is undefined - 07-07-2010 , 09:40 AM






Kevin wrote:

Quote:
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.


--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/

Reply With Quote
  #3  
Old   
Kevin
 
Posts: n/a

Default Re: "GetElementsByTagName().childNodes" is undefined - 07-07-2010 , 02:58 PM



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

Reply With Quote
  #4  
Old   
RobG
 
Posts: n/a

Default Re: "GetElementsByTagName().childNodes" is undefined - 07-07-2010 , 07:20 PM



On Jul 8, 4:58*am, Kevin <kevin.m.mered... (AT) gmail (DOT) com> wrote:
Quote:
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.

<URL: http://www.w3.org/TR/DOM-Level-2-Cor...tml#ID-A6C9094 >


Quote:
* * * * 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-Cor...l#ID-745549614 >

Node:
<URL: http://www.w3.org/TR/DOM-Level-2-Cor...#ID-1950641247 >


Quote:
* * * * 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.


--
Rob

Reply With Quote
  #5  
Old   
Garrett Smith
 
Posts: n/a

Default Re: "GetElementsByTagName().childNodes" is undefined - 07-07-2010 , 08:08 PM



On 2010-07-07 04:20 PM, RobG wrote:
Quote:
On Jul 8, 4:58 am, Kevin<kevin.m.mered... (AT) gmail (DOT) com> wrote:
Martin, thanks for your help.

[snip links, etc]

Quote:
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, ...).

--
Garrett

Reply With Quote
  #6  
Old   
RobG
 
Posts: n/a

Default Re: "GetElementsByTagName().childNodes" is undefined - 07-07-2010 , 09:13 PM



On Jul 8, 10:08*am, Garrett Smith <dhtmlkitc... (AT) gmail (DOT) com> wrote:
Quote:
On 2010-07-07 04:20 PM, RobG wrote:

On Jul 8, 4:58 am, Kevin<kevin.m.mered... (AT) gmail (DOT) com> *wrote:
Martin, thanks for your help.

[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.


--
Rob

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.