mysociety / jquery-multi-select

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

Callback functions needed #17

Open ripetti opened 5 years ago

ripetti commented 5 years ago

I don't find callback functionality. How can I catch events like user select or deselect something? Also drop down or close the list?

thx.

zarino commented 5 years ago

Hi @ripetti, thanks for checking out the plugin.

Apologies if I’m misunderstanding, but why would you need callbacks for the selections/deselections, when you could just listen for events on the underlying <select> element? eg:

$('#people').multiSelect();
$('#people').on('change', function(){
  // $(this).val() will return an array of the selected
  // option values, or null if no options are selected.
});

Callbacks or events for acting on the opening or closing of the dropdown is an interesting idea. Would you expect your callback to fire before or after the dropdown had been opened/closed? And would you need it to be able to prevent the default action (opening/closing the dropdown) from happening?

NicoLPHA commented 4 years ago

Hello

how can i listen to the event when the dropdownlist closes ?? so not onchange but onclose ??

Nico

brockerdock commented 3 years ago

Hello

how can i listen to the event when the dropdownlist closes ?? so not onchange but onclose ??

Nico

Hello, in case you or someone else is still interested:

You can simply set up a function that listens for any click not coming from the drop-down. Because that of course will close the drop-down. So do something like this:

window.onclick = function(event) { if (!event.target.matches('.multi-select-menu')) { \\ write your code here } } Of course this even gets executed if the menu is closed. So you might check before if it is open. You could use the class of the containing div-element and check if that exists: "multi-select-container multi-select-container--open"