I wrote this code for add attr to option element that have defined value on index variable:
$(document).ready(function (){
$('option').each(function() {
var index = '1';
if($(this).attr('value') == index)
$(this).attr('selected','selected');
});
});
How can add attr to each element that have value listed on index variable. Something like this:
var index = '1,2,5,8,7,9';
if($(this).attr('value') == index)
...
UPDATE:
This is my html code:
<select name="category[]" multiple="multiple" id="category">
<option class="level-0" value="1">Jobs</option>
<option class="level-1" value="24">CSS</option>
<option class="level-0" value="5">Products</option>
</select>
Working sample
Without array
Working sample
Using
filter()Working sample
Using
.attr('selected', callback)Working sample