Forum Moderators: open

Message Too Old, No Replies

Difference between firstChild.data and value

         

saarah

8:07 pm on Dec 2, 2007 (gmt 0)

10+ Year Member



Hello,
I'm building my first ajax test application.

I'm returning xml data from my php program and it looks like:


<categories>
<Category>
<id>25</id>
<categoryName>Desktops</categoryName>
</Category>
<Category>
<id>26</id>
<categoryName>Laptops</categoryName>
</Category>
</categories>

I got this piece of code from IBM tutorials to read the xml in javascript:


var xmlDoc = response.responseXML;
var showElements = xmlDoc.getElementsByTagName("Category");
for (var x=0; x<showElements.length; x++)
{
var id = showElements[x].childNodes[0].value;
var category=showElements[x].childNodes[1].value;
}

If I print category or id - I get undefined.
But if I change it to

var id = showElements[x].childNodes[0].firstChild.data;
var category=showElements[x].childNodes[1].firstChild.data;

I'm getting the proper values. So when do we use "value" as opposed to "firstChild.data"

Thanks in advance!

daveVk

9:31 am on Dec 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



firstChild of say "<id>25</id>" gets the child textNode and firstChild.data gives string "25"

value is not defined in this case, it works on attributes for example if coded <Category id=25> then attrNode.value is 25