I'm trying to produce a multiple select list with selected options based from a php array, in jquery. I can't seem to get it working, this is my code:

My select list:

```
<select name="modules[]" id="modules" class="inputbox" size="10" multiple="multiple">
  <option value="1">Module 01</option>
  <option value="2">Module 02</option>
  <option value="3">Module 03</option>
</select>

```

My PHP/JQuery (attempt):

```
<script type="text/javascript" language="javascript">
<?php 
$modules = explode(';',$row->modules);
if (is_array($modules)) {
    foreach($modules as $moduleID) { 
    ?>
    jQuery("#modules").val("<?php echo $moduleID; ?>");
    <?php 
    };
} else { 
?>
jQuery("#modules").val("<?php echo $row->modules; ?>");
<?php 
};
?>
</script>

```

Which looks like (plain jquery):

```
<script type="text/javascript" language="javascript">
  jQuery("#modules").val("3");
  jQuery("#modules").val("1");
</script>

```

And this is my php array (after being exploded):

```
Array
(
    [0] => 3
    [1] => 1
)

```