0

I am trying to create and populate a HTML select and option tags using JavaScript. The text to be used within the option statements comes from an XML file. I am able to parse the XML file problem free yet I am stuck when it comes to populating the new select box. Despite any changes I have attempted with this code it does not work. Does anyone have any insight on what I am doing wrong here?

My XML file:

<londonStreets>
 <street>Clarence Street</street>
 <street>Dundas Street</street>
 <street>King Street</street>
 <street>Queens Avenue</street>
 <street>Richmond Street</street>
 <street>Waterloo Street</street>
 <street>Wellington Street</street>
 <street>York Street</street>
</londonStreets>

My JavaScript that I figured would create the new select box:

var streetSelector=document.createElement('select');
streetSelector.setAttribute('id', 'street');
for (i=0;i<x.length;i++)
{
 var option=document.createElement('option');
 option.setAttribute('value', x[i].childNodes[0].nodeValue);
 option.appendChild(document.createTextNode(x[i].childNodes[0].nodeValue));
 streetSelector.appendChild(option);
}
Kevin Babcock
10.3k20 gold badges72 silver badges90 bronze badges
asked Jul 1, 2013 at 20:26
2
  • 1
    Good for you, using regular JavaScript :) Commented Jul 1, 2013 at 20:31
  • Did you append the streetSelector to some HTML element (body, div, etc?) Commented Jul 1, 2013 at 20:33

2 Answers 2

2

You have not appended streetSelector to your DOM. See this JS fiddle:

document.getElementById("content").appendChild(streetSelector);

http://jsfiddle.net/Cszm8/

answered Jul 1, 2013 at 20:36
Sign up to request clarification or add additional context in comments.

Comments

1

without seeing what x is exactly, here is a simplified options populator routine:

var streetSelector=document.createElement('select');
streetSelector.id='street';
for (i=0;i<x.length;i++){
 streetSelector.appendChild(new Option( x[i].childNodes[0].nodeValue));
}
answered Jul 1, 2013 at 20:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.