0

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..

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.