if (xhr) {
xhr.addEventListener("readystatechange",setStatesArray,false);
xhr.open("GET", "us-states.xml", true);
xhr.send(null);
}
else {
alert("Sorry, but I couldn't create an XMLHttpRequest");
}
}
function setStatesArray() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (xhr.responseXML) {
var allStates = xhr.responseXML.getElementsByTagName("item");
for (var i=0; i<allStates.length; i++) {
statesArray[i] = allStates[i].getElementsByTagName("label")[0].firstChild;
}
}
}
else {
alert("There was a problem with the request " + xhr.status);
}
}
}
function searchSuggest() {
var str = document.getElementById("searchField").value;
document.getElementById("searchField").className = "";
if (str != "") {
document.getElementById("popups").innerHTML = "";
for (var i=0; i<statesArray.length; i++) {
var thisState = statesArray[i].nodeValue;
if (thisState.toLowerCase().indexOf(str.toLowerCase()) == 0) {
var tempDiv = document.createElement("div");
tempDiv.innerHTML = thisState;
tempDiv.addEventListener("click",makeChoice,false);
tempDiv.className = "suggestions";
document.getElementById("popups").appendChild(tempDiv);
}
}
var foundCt = document.getElementById("popups").childNodes.length;
if (foundCt == 0) {
document.getElementById("searchField").className = "error";
}
if (foundCt == 1) {
document.getElementById("searchField").value =
document.getElementById("popups").firstChild.innerHTML;
document.getElementById("popups").innerHTML = "";
}
}
}
<?xml version="1.0"?>
<choices xml:lang="EN">
<item><label>Alabama</label><value>AL</value></item>
<item><label>Alaska</label><value>AK</value></item>
<item><label>Arizona</label><value>AZ</value></item>
<item><label>Arkansas</label><value>AR</value></item>
<item><label>California</label><value>CA</value></item>
<item><label>Colorado</label><value>CO</value></item>
<item><label>Connecticut</label><value>CT</value></item>
<item><label>Delaware</label><value>DE</value></item>
<item><label>Florida</label><value>FL</value></item>
<item><label>Georgia</label><value>GA</value></item>
<item><label>Hawaii</label><value>HI</value></item>
<item><label>Idaho</label><value>ID</value></item>
<item><label>Illinois</label><value>IL</value></item>
<item><label>Indiana</label><value>IN</value></item>
<item><label>Iowa</label><value>IA</value></item>
<item><label>Kansas</label><value>KS</value></item>
<item><label>Kentucky</label><value>KY</value></item>
<item><label>Louisiana</label><value>LA</value></item>
<item><label>Maine</label><value>ME</value></item>
<item><label>Maryland</label><value>MD</value></item>
<item><label>Massachusetts</label><value>MA</value></item>
us-states.xml