0

In a success AJAX return I am trying to loop through an new array (traits), and used each item from that array for a new data string to get individual items from the AJAX returned array. So I need to dynamically look for data.trait_return.background_path, data.trait_return.face_path, data.trait_return.body_path etc. How to I make the var trait_path dynamic to work in the JSON data return string to look it an item from the returned JSON array? Right now I tried var trait_path = trait + "_path"; with no luck. Hope that makes sense.

$.ajax({
        url         : 'start.php',
        type        : 'POST',
        data        : form_data,
        dataType    : "JSON",
        success     : function (data) {

            var traits = ["background","face","body","back","ears","head","bandana","legs"];
            $.each(traits, function(index, trait) {
                var trait_path = trait + "_path";
                alert(trait_path);
                $('#'+trait+'_select').val(data.trait_return.trait_path).prop("selected", "selected");
            })
        }
})
Chris
  • 833
  • 2
  • 17
  • 37

0 Answers0