webguy
webguy

Reputation: 692

Add options to select box via json

I'm trying to modify some jquery, but I'm stuck.

I need to append to a series of options to an already existing select box with some existing options.

The code below creates an ENTIRE select box with options:

$.getJSON( "sampleText.txt", function( data ) {
    var items = [];
    $.each( data, function( key, val ) {
        items.push( "<option value='" + key + "'>" + val + "</option>" );
    });

    $( "<select/>", {
        html: items.join( "" )
    }).appendTo( "#smsMsgList" );
});

... but I just want to append multiple options to my EXISTING select box.

Bascially, I need to add the "html: items.join("")" and the ".appendTo( "#smsMsgList" );" without creating the surrounding select box.

Any help appreciated!

Upvotes: 0

Views: 1427

Answers (1)

Reigel Gallarde
Reigel Gallarde

Reputation: 65254

$.getJSON( "sampleText.txt", function( data ) {
  var items = "";
  $.each( data, function( key, val ) {
    items += "<option value='" + key + "'>" + val + "</option>";
  });

  $(items).appendTo( "#smsMsgList" );
});

given that select has an id of smsMsgList

Upvotes: 2

Related Questions