var colorID = 3;
$('#color').each(function(){
if($('#color').val() == colorID )
$('#color').attr('selected');
});
Why doesn’t this work?
My html
<select id="color"><option value="">Select Color</option>
<option value="1">Red</option>
<option value="2">Blue</option>
<option value="3">Black</option>
<option value="4">Purple</option>
</select>
You can just use
.val()to get/set the value of any input style element, including a<select>:You can test it out here. For illustration here’s a working long-hand version of what you were attempting:
You were looping through but then checking the
<select>, not the<option>elements, which doesn’t have those properties.