alyx
alyx

Reputation: 2733

jQuery - Populating drop down form option values

I'm able to populate the names of option fields for the #maplist select dropdown:

$.each(mapsArray, function(x,y) {

    $.each(y, function(j,z) { 

        $('#maplist').append(

            $('<option></option>').html(z.mapName)

        );
    });
});

But I can't figure out how to add .val to each option field (in this case, it would be z.mapID.$id)

Upvotes: 3

Views: 1040

Answers (3)

Gabe
Gabe

Reputation: 50493

I would do it this way rather than concatenating a bunch of strings.

$.each(mapsArray, function(x,y) {
    $.each(y, function(j,z) { 
       $('#maplist').append(
          $('<option></option>').val(z.mapName).text(z.mapName);
       );
    });
});

Upvotes: 1

Jason
Jason

Reputation: 11615

Use .attr():

http://api.jquery.com/attr/

$.each(mapsArray, function(x,y) {
    $.each(y, function(j,z) { 
        $('#maplist').append(
            $('<option></option>').html(z.mapName).attr('value', z.mapID.$id);
        );
    });
});

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

$.each(mapsArray, function(x,y) {

    $.each(y, function(j,z) { 

        $('#maplist').append(

            $('<option value="'+ z.mapName +'">'+ z.mapName +'</option>');

        );
    });
});

Upvotes: 2

Related Questions