-1

I have ajax function on aspx page.

 function fav() {

            var p = "3";

            var e = document.getElementById('<%= ddlCountry.ClientID %>');  
            var j = e.options[e.selectedIndex].text;

                //var aralik = p[1] + p[2];

                $.ajax({
                    type: "POST",
                    url: "STry.aspx/Fill",
                    data: "{'Param': '" + p + "','Param2': '" + j + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    error: function (xhr, status, error) {
                        //alert the error if needed              
                    },
                    success: function (msg) {

                        var data = msg.d;

                        if (data.indexOf(',') < 0)
                        {
                            document.getElementById('<%= ddlCity.ClientID %>').options.add(data);
                        }
                        else
                        {
                            data.split(",").forEach(function (item) {
                                var opt = document.createElement(item);
                                document.getElementById('<%= ddlCity.ClientID %>').options.add(opt);
                            });
                        }

                    }

                });

        }

the data return var data = msg.d; so, my return data is data. data type like this, if one more than;

text1,text2,text3,text4

and i want to split this value and add dropdownlist that is ddlCity

this part is my try:

var data = msg.d;

      if (data.indexOf(',') < 0)
        {
          document.getElementById('<%= ddlCity.ClientID %>').options.add(data);
        }
         else
         {
            data.split(",").forEach(function (item) {
            var opt = document.createElement(item);
            document.getElementById('<%= ddlCity.ClientID %>').options.add(opt);
         });

but it is not working.

Briefly, I have a return data like (text1,text2,text3,text4) how to add return data in ddlCity item on javascript?

Thank you for your answers.

jeremmy p
  • 49
  • 4

2 Answers2

1

Try this

data.split(",").forEach(function (item) {
var option = document.createElement("option");
option.text = item;
option.value = item;
var ddl= document.getElementById('<%= ddlCity.ClientID %>')
ddl.appendChild(option);
});

Check below append option to select menu?

Community
  • 1
  • 1
malkam
  • 2,337
  • 1
  • 14
  • 17
1

Using forEach as in your code snippet:

Demo: http://jsfiddle.net/abhitalks/63eX6/1/

var data = "text1,text2,text3,text4";
var ddl = document.createElement("select");
ddl.id = "s1";
document.getElementById("container").appendChild(ddl);
data.split(",").forEach(function(item) {
    var opt = document.createElement("option");
    opt.value = item;
    opt.text = item;
    ddl.appendChild(opt);
});
  1. Create a select, assign an id to it, and append it to an existing container like div,
  2. Loop over the split array, create option element, assign value and text and then append it to the select.
Abhitalks
  • 27,721
  • 5
  • 58
  • 81