JQuery - parameter values ​​of the dropdown fill form

I can fill in the parameter field names for the drop-down list #maplist:

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

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

        $('#maplist').append(

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

        );
    });
});

But I can’t understand how to add .valoptions to each field (in this case it will be z.mapID.$id)

+3
source share
3 answers

I would do it like this rather than concatenating a string of strings.

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

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

        $('#maplist').append(

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

        );
    });
});
+2
source

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);
        );
    });
});
0
source

All Articles