0

I have a javascript script that allows me to select data from drop down menus, how would I modify my script to be able to select from an array instead of just a single value?

<script type='text/javascript'>
$(window).load(function(){
    $("select[value]").each(function() {
        $(this).val(this.getAttribute("value"));
    });
}); 
</script>

In the HTML code normally I would just put the value as the data from the database, and it would auto select it. But with a multi select, I have it in the database as

option1,option2,option3

And then I turn it into an array

Array
(
    [0] => option1
    [1] => option2
    [2] => option3
)

So how would I tell my script to select each and every value in the array?

eggyal
  • 122,705
  • 18
  • 212
  • 237
WittyPleb
  • 553
  • 2
  • 10
  • 22
  • 3
    possible duplicate of [JQuery multiselect - Set a value as selected in the multiselect dropdown](http://stackoverflow.com/questions/6966260/jquery-multiselect-set-a-value-as-selected-in-the-multiselect-dropdown) – Manse Apr 25 '12 at 15:18
  • 1
    Thanks, I didn't search very well :( That got it to work after a bit of tweaking – WittyPleb Apr 25 '12 at 15:30

1 Answers1

1

You can just set the value to your array:

$('#mySelect').val(myArray)
Joel
  • 6,193
  • 6
  • 22
  • 22