user3260246
user3260246

Reputation: 1

how to bind data from database to select tag of html using jquery

Can anyone help me out in binding data from database to select tag of html using jquery with an example.

My requirement is to get one column from database table named 'Purpose' (consists of records such as lunch, pizza, tea, coffee etc..) into select tag either by using jquery or javascript. I tried binding values to control from code behind but with runat=server property. I want to bind the same without using runat=server property, but by using jquery or javascript.

Looking forward for reply..

Upvotes: 0

Views: 1195

Answers (1)

schei1
schei1

Reputation: 2487

To populate a <select> with jQuery with data from an external url:

var url = "path/to/some/json/resource"; // the url to load data from.
$.getJSON( url, function(result) {
    var $select = $('#myDropDown'); // the <select> element.
    $select.empty(); // probably want to clear before populating.
    $.each(result, function() {
        // append an <option> for each item in returned list.
        select.append($("<option></option>").attr(result.PurposeId).text(result.Name)); 
    });
});

You would probably want to load data from a WebAPI, IHttpHandler etc, where you can specify the returned format.

Upvotes: 1

Related Questions