Display all nodes inside a tagname with JavaScript

Yanol

New Member
I'm trying to use the loadXMLDoc() JavaScript function to load data from an XML document and show it on my page's DIV when a button is clicked. So far I can't get any content in my DIV, I want to load all elements within an specific tagname.Here's the XML:\[code\]<?xml version="1.0" encoding="UTF-8"?><Fruits><Cell>Apples</Cell><Cell>Bananas</Cell><Cell>Strawberries</Cell></Fruits><Vegetables><Cell>Lettuce</Cell><Cell>Tomatoes</Cell><Cell>Carrots</Cell></Vegetables>\[/code\]Here's the JavaScript:\[code\]function fruits() {var xmlhttp; if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { xmlDoc = xmlhttp.responseXML;document.getElementById("food").innerHTML=xmlDoc.getElementsByTagName("fruits"); } xmlhttp.open("GET","document.xml", true); xmlhttp.send(); }function vegetables() {var xmlhttp; if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { xmlDoc = xmlhttp.responseXML;document.getElementById("food").innerHTML=xmlDoc.getElementsByTagName("vegetables"); } xmlhttp.open("GET","document.xml", true); xmlhttp.send(); }\[/code\]And here's the HTML:\[code\]<button type="button" onclick="fruits()">Fruits</button><button type="button" onclick="vegetables()">Vegetables</button><div id="food">Please select your favorite</div>\[/code\]Please help me geeks, thank you so much!
 
Back
Top