$('#sellist option').each(function(index) {
var str ='4/5/8';
var substr = str.split('/');
if (substr[0] == $(this).attr('value')) {
$(this).attr('selected','selected');
alert('hi'); ///For check
}
});
Every thing works fine and alert is fired. I havent any error in console. But #sellist option is not selected which I want. What is my problem. Thanks.
Your code is working fine. It is just selecting the first element of your array,
4because you are not looping through the array, you just usesubstr[0]to get the first value.To see that it works I changed the initial value to
7:Fiddle to show it works
OP Clarified he want to loop around array
To loop through your array and select each value in the multiple select, use this:
Update fiddle