With Jquery, I'm looping through the attribute named tags
, in each <option>
of the <select>
dropdown below.
You'll see I would LOVE to have 2 of the options preselected, if the number 6 exists in the tags
attribute.
<select name="someFun" id="someFun" multiple="multiple">
<option value="1" tags="4,6,7">something</option>
<option value="44" tags="2">something</option>
<option value="61" tags="1,3,6">something</option>
<option value="44" tags="2">something</option>
</select>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
var preselectedtag = '6'; // auto select any with 6
$("#someFun > option").each(function() {
var tag = $(this).attr("tags");
/* change attr of this option to "selected" if 6 exists in var tag */
});
});
</script>
I could use some help here. Is there a best way to do this?
In PHP I normally use an in_array() function, but javascript seems way more picky.