0

Im trying to populate a dropdown list created in HTML with data i am matching from an XML file.

At the moment I can read the XML file and get the matching data i want using a javascript function and display the variable in an Alert window for debugging purposes.

However, I am unsure how to bind each piece of matching data to the dropdown list.

Below is the XML searching Function showing the alert popup and the variable "name", I want to display each entry of "name" as n option in the dropdownlist.

if (xmlDoc != null) {      
    var restaurants = new Array();
    var x = xmlDoc.getElementsByTagName("Units");
    for (i = 0; i < x.length; i++) {
        if (x[i].getElementsByTagName("Type")[0].childNodes[0].nodeValue == "Container" ) {

           if (x[i].getElementsByTagName("Allocated")[0].childNodes[0].nodeValue == "YES" ) {

            var name = x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue;
            var telnumber = x[i].getElementsByTagName("Allocated")[0].childNodes[0].nodeValue;               
                  alert("Found "+name); 
         }
       }

And here is my HTML dropDownlist populate with one "default" entry.

<select id="myDropDown" style="width: 95%; border-radius:8px; border:1px solid #AAAAAA;"><option value="default">Default</option></select>

Im just not sure how to get my "name" variable in there and populate a list.

Any help would be appreciated.

2
  • Wouldn't this existing answer give you what you need? stackoverflow.com/questions/23573407/… Or if you'd rather use pure JS (i.e., without jQuery), try this: aspsnippets.com/Articles/… Commented Jan 19, 2019 at 15:52
  • Hi Marc, Thank you, that worked well!! I went for the pure JS method. Thanks Commented Jan 19, 2019 at 16:08

0

Your Answer

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.