Archer
Archer

Reputation: 1162

storing the json data for options of a select tag, in a variable

Good day to you all

I am retrieving the options for my select tag in json format and populating the select tag using this

var mySelect = $('#'+select_id)
    $.each(response, function(key,val){
    $('<option/>',{
        value : key
    })
    .text(val)
    .appendTo(mySelect);
    });

Which works fine, but i want to store the html string generated out of the conversion from the json array into a variable, instead of appending it to the select tag. How can i do that?

Upvotes: 1

Views: 792

Answers (1)

pdoherty926
pdoherty926

Reputation: 10349

You can accomplish this with jQuery.map. http://api.jquery.com/jQuery.map/

var result = $.map(response, function(key,val){
    return "<option value='" + key + "'>" + val + "</option>";
}).join('');

http://jsfiddle.net/DPZGR/

Upvotes: 1

Related Questions