Firstly, your selector is not ideal if you have multiple <select>
elements on the page. You’d be better to use #maschAusw
.
You have to specify the selected
attribute. You can do something like this:
$("#maschAusw").children('option[value="opt2"]').prop('selected', true);
You should also remove the selected
attribute from the currently selected option, which can be done as follows:
$("#maschAusw").children('option[value="opt2"]')
.prop('selected', true)
.siblings()
.prop('selected', false);
Please see the working example below:
const data = ['opt1', 'opt2', 'opt3'];
for (let i = 0; i < data.length; i++) {
console.log(data[i]);
$('#maschAusw').append($("<option>").attr('value', data[i]).text(data[i]));
}
$("#maschAusw").children('option[value="opt2"]')
.prop('selected', true)
.siblings()
.prop('selected', false);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="maschAusw">
<option selected disabled value="">Choose...</option>
</select>