Using foreach inside an ajax function

Ok so it actually looks like there is a couple of ways you could do this. The first one is by changing your $list into an array and returning it as JSON.

It will be easy to change $list to an array by just adding it like this:

$list[] =  '<option value="'.$term->term_id.'">'.$term->name.'</option>';

Then return it as JSON: echo json_encode( $list );

This may give you more flexibility within your JavaScript code if you need to perform other actions on specific options.

The other way you might go about this is keep the code as you have it. It looks like the way you currently have it setup you will get all of the added <option> tags in one giant string. This should be ok for jQuery to handle. You just need to do something with it in your response:

    success: function( data ){
                console.log(data);
                $('#yourSelectBox").html(data);
            }

That would add the giant long string of <option>s you got from the AJAX to be added as the HTML inside the <select> and </select> of your select box.