Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want create dynamic dropdownlist on function call in javascript and fill the drodownlist with database with help of ajax

What I have tried:

JavaScript
function dropdownlistbind(cn, dropdownid, tablename) {
    var obj = {};
    obj.coluname = cn;
    obj.tablename = tablename;
    $.ajax({
        url:         "webmethod.aspx/dropdownbine",
        data:        JSON.stringify(obj),
        dataType:    "json",
        type:        "POST",
        contentType: "application/json; charset=utf-8",
        success: function(r) {
            var data = new Array();
            var len = r.d.length;
            for (var i = 0; i < len; i++) {
                data[i] = r.d[i];
            }
            alert(data);
            return data;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert('Got an Error ');
        }
    });
}

function addSelect(divname, columname, dropid, tablename) {
    var newDiv = document.createElement('div');
    var strin = "ffgggffggf";
    var html = '<select>', filter = dropdownlistbind(columname, dropid, tablename), i;
    alert();
    for (i = 0; filter.length; i++) {
        html += "<option value='" + filter[i] + "'>" + filter[i] + "</option>";
    }
    html += '</select>';
    newDiv.innerHTML = html;
    document.getElementById(divname).appendChild(newDiv);
}
Posted
Updated 16-Aug-16 2:32am
v2
Comments
njammy 16-Aug-16 8:17am    
Please can you post text of the error you are getting. Or take a screenshot and upload to imgur share link.
Nethaji chennai 16-Aug-16 8:49am    
dropdown list bind empty data.first run filter loop then display alert msg alert display
njammy 16-Aug-16 8:53am    
Okay......so I ask again...what is the error

1 solution

You cant return the value from Success callback function of ajax call, instead you should write all your logic inside success callback.

JavaScript
function addSelect(divname, columname, dropid, tablename) {

           var newDiv = document.createElement('div');
           var html = '<select>'
           var obj = {};
           obj.coluname = cn;
           obj.tablename = tablename;

           $.ajax({
               url: "webmethod.aspx/dropdownbine",
               data: JSON.stringify(obj),
               dataType: "json",
               type: "POST",
               contentType: "application/json; charset=utf-8",
               success: function (r) {

                   var result = r.d;
                   for (i = 0; i < result.length; i++)
                       html += "<option value='" + result[i] + "'>" + result[i] + "</option>";

                   html += '</select>';
                   newDiv.innerHTML = html;
                   document.getElementById(divname).appendChild(newDiv);

               },
               error: function (XMLHttpRequest, textStatus, errorThrown) {
                   alert('Got an Error ');
               }
           });

       }
 
Share this answer
 
Comments
Nethaji chennai 16-Aug-16 9:15am    
thankyou Karthik its work
Karthik_Mahalingam 16-Aug-16 9:17am    
cool.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900