View Full Version : Javascript Dropdown list with XML file

09-25-2011, 11:19 PM
I just recently bought 2 books, AJAX and Javascript, to help me in writing a wep application. They have been great so far except for one thing. I am trying to populate a dropdown list with an XML file, but I want it to load into the dropdown list only AFTER a radio button has been clicked. Does anyone know how to do this or does anyone know where I could go to get some insight on this? Thanks.

09-26-2011, 05:04 AM
it's not that complicated, but it is a little hard to know what's going on with your code without actually seeing any of it.

anyway. let's suppose that:
a) you have extracted the data from the xml file,
b) turned it into objects and pushed them onto an array, and
c) that array is called "widgets"

next you set up an "empty" select list, inside a form like this:

<form name="myform"><select id="selectlist"><option>select an option</option></select></form>

then with the onclick from your radio button you can call a function something like this:

function fillList() {
for (var k=0; k<widgets.length; k++) {
var optn = document.createElement("OPTION");
optn.text = widgets[k];

of course, it all depends on what you want the dropdown to do and how you want it to relate to your objects - you could just as easily say:
optn.text = widgets[k].name;
if indeed your widgets did have a name
and you could also add something like
optn.value = widgets[k].price;
if you wanted to do something with the value field in the dropdown.

hope that helps. If things start going sideways post some code :thumbsup: