Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 955
alyx Avatar asked May 16 '26 21:05

alyx


2 Answers

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

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

        $('#maplist').append(

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

        );
    });
});
like image 98
thecodeparadox Avatar answered May 18 '26 12:05

thecodeparadox


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);
       );
    });
});
like image 21
Gabe Avatar answered May 18 '26 11:05

Gabe