1

I have this string:

var product = "bj,tg,ap,nl";

Now I´d like jQuery to select these values in a select2 option list. I know how to select just one:

$('#product').val('bj').trigger('change.select2');`

But how do i select all the comma separated values from that string? I guess it should look like:

$('#product').val('bj', 'tg', 'ap').trigger('change.select2');

So I need to put some ' and spaces into my string "product". How would i do that? Can i use regex?

1
  • Can you provide a title that remotely matches what your asking please? Commented Aug 1, 2017 at 9:30

1 Answer 1

8

.val() method accepts an array for multiple option. You can use split() to create an array and pass it to .val() method.

$('#product').val(product.split(',')).trigger('change.select2');
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. I like it when it´s this easy! Working like a charm. Thanks a lot!

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.