/    Sign up×
Community /Pin to ProfileBookmark

js+ajax+dom

hi

this is the xml file i am using

<?xml version=”1.0″ encoding=”UTF-8″?>
<states>
<North>
<state>Minnesota</state>
<state>Iowa</state>
<state>North Dakota</state>
</North>
<South>
<state>Texas</state>
<state>Oklahoma</state>
<state>Louisiana</state>
</South>
<East>
<state>New York</state>
<state>North Carolina</state>
<state>Massachusetts</state>
</East>
<West>
<state>California</state>
<state>Oregon</state>
<state>Nevada</state>
</West>
</states>

and my html file is

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<title>Parsing XML Responses with the W3C DOM</title>
<script type=”text/javascript”>
var xmlHttp;
var requestType = “”;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}

function startRequest(requestedList) {
requestType = requestedList;
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open(“GET”, “parseXML.xml”, true);
xmlHttp.send(null);
}
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
var box=document.getElementById(“mySelect”);
var region=box.options[box.selectedIndex].text;
//alert(region);
listAllStates(region);
}
}
}

function listAllStates(region) {
//alert(region);
var xmlDoc = xmlHttp.responseXML;
var selectNode=xmlDoc.getElementsByTagName(region);
//var allStates = selectNode.getElementsByTagName(“state”);
outputList(“All States in Document”, selectNode);
}

function outputList(title, states) {
var out=null;
var stu=new Array();
var currentState = null;
var child=states[0].getElementsByTagName(“state”);
for(var i = 0; i <child.length; i++) {
currentState = child[i].nodeValue;
stu[i]=currentState;
}
for(var i=0;i<stu.length;i++)
document.write(stu[i]);
}
</script>
</head>
<body>
<h1>Process XML Document of U.S. States</h1>
<br/><br/>

<form action=”#”>
<select name=”mySelect” onchange=”startRequest()”>
<option name=”North”>North</option>
<option name=”South”>South</option>
<option name=”East”>East</option>
</select>
</form>
</select>
</form>
</body>
</html>

when i select a region form the drop down menu it should list me the states in the region. eg if i select ‘north’ it should display states in north from the xml file.
but when ever i select a region it displays null, i dont know what i am doing wrong
pls help

thanks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterFeb 11.2007 — nodeValue and data are text node properties not element properties so you will need to go one step further and select the actual textNode unlike the IE-only xml property for xml elements which is similar to the innerHTML property in HTML which goes on the element rather than the textNode unlike nodeValue.
<i>
</i>currentState = child[i][color=blue].firstChild[/color].nodeValue;
Copy linkTweet thisAlerts:
@pop3authorFeb 11.2007 — Thanks you ery much
×

Success!

Help @pop3 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...