You must Sign In to post a response.
  • Category: JQuery

    How to add Text and Value in dropdownist using JQuery


    Are you looking for a way to add Text and Value in dropdownist using JQuery ? then read this thread to know how to add it



    hi folk,

    <option value="cloth">cloth</cloth>
    i want to bind dropdownlist value property to category id not for text

    <option value="1">cloth</cloth>
    my current jquery code is

    ddlcategory.append($("<option ></option>").val(this['Value']).html(this['Text']));

    how do i change in jquery

    thank you in advance

    regards,
    abhijit patil
    http://dotnetbyabhipatil.blogspot.in
  • #763444
    Did you try the following?

    $('#ddlcategory').append($('<option>', {
    value: 1,
    text: 'cloth'
    }));

    By Nathan
    Direction is important than speed

  • #763445
    hey nathan

    i want to dynamically add value to dropdownlist

    so how i do change in jquery code

    var ddlcategory = $("[id*=ddlcategory]");
    ddlcategory.empty().append('<option selected="selected" value="0">Please select</option>');
    $.each(r.d, function () {
    ddlcategory.append($("<option></option>").val(this['Value']).html(this['Text']));
    });


    thank you in advance

  • #763447
    Hello Abhijit Patil,

    You can use this code :

    $('ddlcategory').val('someval').prop('selected', true);

    Hope this will help you to set value property to your control using JQuery. Let me know if you still facing the problem.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "If you can dream it, you can do it."

  • #763448
    dear narav

    not working .not shown any value in dropdownlist

    could you please change in my jquery code
    <script type="text/javascript">
    $(function () {
    $.ajax({
    type: "POST",
    url: "bindcategoryvaluetodropdown.aspx/binddatachecklistbox",
    data: '{}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",

    success: function (r) {
    var ddlcategory = $("[id*=ddlcategory]");
    ddlcategory.empty().append('<option selected="selected" value="0">Please select</option>');
    $.each(r.d, function () {
    // ddlcategory.append($("<option ></option>").val(this['Value']).html(this['Text']));
    $('ddlcategory').val('someval').prop('selected', true);

    });
    }
    });
    });
    </script>
    ----------------webmethod--------
    [WebMethod]
    public static List<ListItem> binddatachecklistbox()
    {
    string query = "SELECT CategoryId,CategoryName FROM Category ";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
    using (SqlCommand cmd = new SqlCommand(query))
    {
    List<ListItem> customers = new List<ListItem>();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    con.Open();
    using (SqlDataReader sdr = cmd.ExecuteReader())
    {
    while (sdr.Read())
    {
    customers.Add(new ListItem
    {
    Value = sdr["CategoryName"].ToString(),
    Text = sdr["CategoryName"].ToString()
    });
    }
    }
    con.Close();
    return customers;
    }
    }
    }

    thank you in advance

  • #763452
    JQuery has '.val' and '.text' methods defined, you can use them accordingly
    see below links
    http://www.c-sharpcorner.com/Blogs/13350/bind-dropdownlist-in-Asp-Net-using-jquery.aspx
    http://www.aspdotnet-suresh.com/2012/07/how-to-bind-dropdownlist-in-aspnet.html

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments