Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preselecting option in multiselect using val()

Tags:

jquery

How do you preselect an option in a multiple select html tag using jquery's val()?

What I mean by this is that say you have a multiselect component and you want some of the values to be preselected?

like image 837
oneiros Avatar asked Aug 10 '26 19:08

oneiros


1 Answers

Assuming I've understood your question correctly, you can pass an array of values to the .val() method:

$("#yourSelect").val(["value1", "value2"]);

Here's a working example.

like image 53
James Allardice Avatar answered Aug 12 '26 08:08

James Allardice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!