john
john

Reputation: 2853

Jquery select all values in a multiselect dropdown

I have a multi-select html dropdown. I want to programatically (via jquery) select all values in the dropdown. How can I do this?

Upvotes: 9

Views: 20348

Answers (2)

Richard Dalton
Richard Dalton

Reputation: 35793

Where the select has the id test

$('#test option').attr('selected', 'selected');

JSFiddle Example

Or as Ryan mentioned you can use .prop

$('#test option').prop('selected', true); // or pass false to deselect them

Upvotes: 21

Jon
Jon

Reputation: 437336

Simply do $("#dropdown").val([0, 1, 2, 3]) where 0, 1, 2, 3 the values you want to select.

Upvotes: 0

Related Questions