I have the following HTML:
$('#select_id option:eq(0)').prop('selected', 'selected');
its good
If you don't mind modifying your HTML a little to include the value
attribute of the options, you can significantly reduce the code necessary to do this:
<option>B</option>
to
<option value="B">B</option>
This will be helpful when you want to do something like:
<option value="IL">Illinois</option>
With that, the follow jQuery will make the change:
$("select option[value='B']").attr("selected","selected");
If you decide not to include the use of the value
attribute, you will be required to cycle through each option, and manually check its value:
$("select option").each(function(){
if ($(this).text() == "B")
$(this).attr("selected","selected");
});
You can use pure DOM. See http://www.w3schools.com/htmldom/prop_select_selectedindex.asp
document.getElementById('dropdown').selectedIndex = 1;
but jQuery can help:
$('#dropdown').selectedIndex = 1;
Something along the lines of...
$('select option:nth(1)').attr("selected","selected");
<select id="cars">
<option value='volvo'>volvo</option>
<option value='bmw'>bmw</option>
<option value='fiat'>fiat</option>
</select>
var make = "fiat";
$("#cars option[value='" + make + "']").attr("selected","selected");
$(document).on('change', 'select', function () {
var value = $(this).val();
$(this).find('option[value="' + value + '"]').attr("selected", "selected");
})