mysociety / jquery-multi-select

Converts <select multiple> elements into dropdown menus with checkboxes
https://www.mysociety.org/
Other
24 stars 18 forks source link

Select / Deselect from other function #26

Open brockerdock opened 3 years ago

brockerdock commented 3 years ago

Depending on other user input on other parts of a webpage I would like to select/deselect entries in the multi-select. How can I do this?

brockerdock commented 3 years ago

I just figured out the syntax to change the selection state of the multi-select. It would be:

$('#Fruits').val(["0","4"]); $("#Fruits").data("plugin_multiSelect").updateMenuItems();

So that would get the 1st and 5th elemnent of the drop-down "Fruits" selected.

gregfr commented 2 years ago

Thanks for this tip; how do you update the label (the concatenated string of options which is show when the select is not open) ? it seems the the updateMenuItems() updates only the content, not the label...

zarino commented 2 years ago

@gregfr .updateButtonContents() updates the text label inside the button: https://github.com/mysociety/jquery-multi-select/blob/ed762f7582a73702a29208f197a065cfbaff37f0/src/jquery.multi-select.js#L128

gregfr commented 2 years ago

It seems that updateButtonContents() does the job

gregfr commented 2 years ago

@gregfr .updateButtonContents() updates the text label inside the button:

https://github.com/mysociety/jquery-multi-select/blob/ed762f7582a73702a29208f197a065cfbaff37f0/src/jquery.multi-select.js#L128

Thanks for the fast answer :-D