Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Thanked 0 Times in 0 Posts

    Javascript Dropdown list with XML file

    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.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Thanked 568 Times in 565 Posts
    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


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts