Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    1
    Thanks
    0
    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
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 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:

    Code:
    <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:

    Code:
    function fillList() {
    for (var k=0; k<widgets.length; k++) {
    var optn = document.createElement("OPTION");
    optn.text = widgets[k];
    document.myform.selectlist.options.add(optn);
    	}
    }
    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
    •