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?

2

1 Answer 1

1

You can just set the value to your array:

$('#mySelect').val(myArray)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.