/    Sign up×
Community /Pin to ProfileBookmark

Options constructor

Im trying to use the information being imported from my XML document to create market-up form my drop down box. The idea is to create and <option> element with the length of that the States information im importing from the XML document. So that lets say i add/delete state information to my XML document it will add/delete that <option> from my drop down list on my HTML page.

Here is what i have so far

i’am able to get one of the states to show. and i believe it wants to do the other but my code is only creating one instance of createElement(“option”). i don’t wanna type this code a gain and again. it defeats the purpose.

html document

[CODE]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=’http://www.w3.org/1999/xhtml’ xml:lang=’en’>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>DropDownBox</title>

</head>
<script type=”text/javascript”>

window.onload = startRequst;
var xmlHttp;

function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}

function startRequest(){
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open(“GET”, “parseXML.xml”, true);
xmlHttp.send(null);
}

function handleStateChange(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
dropDownBox();
}
}
}

function dropDownBox(){
var xmlDoc = xmlHttp.responseXML;
var northNode = xmlDoc.getElementsByTagName(“north”)[0];
var northStates = northNode.getElementsByTagName(“state”);
for (var i = 0; i < northStates.length; i++) {
var states = northStates[i].childNodes[0].nodeValue;
alert(states);
var state = document.createTextNode(states);
var container = document.createElement(“option”);
container.appendChild(state);
}
document.getElementsByTagName(“select”)[0].appendChild(container);
}

</script>
<body>
<h1>Process XML Document of U.S. States</h1>
<br/><br/>
<form action=”#”>
<br/><br/>

<select>
//This is the selection filled i am trying options to.:confused:

</select>
</form>
</body>

</html>
[/CODE]

Xml document

[CODE]
<?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>Massahusetts</state>
</east>
<west>
<state>California</state>
<state>Oregon</state>
<state>Navada</state>
</west>
</states>
[/CODE]

I found this information below but i don’t know how to apply it to my item

[url]http://www.javascriptkit.com/javatutors/selectcontent.shtml[/url]
When repopulating a select element’s content, you may want to use a loop instead of changing each option one by one. Furthermore, the total number of options may not be known in advance (such as 3 above), so you need a dynamic way to add new options to select. In this case, use the below:

var master=document.myform.master
for (i=0; i<somevariable; i++){
master.options[master.options.length]=new Option(…)
}

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@WebnerdJul 04.2010 — Try changing your code:

<i>
</i> for (var i = 0; i &lt; northStates.length; i++) {
var states = northStates[i].childNodes[0].nodeValue;
var container = new Option(states,states);
document.getElementsByTagName("select")[0].options[i] = container;
}


Copy linkTweet thisAlerts:
@uknowmeimauthorJul 04.2010 — THANK YOU! I have been trying to figure this out for a whole week. THANK YOU!
×

Success!

Help @uknowmeim 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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