fabianlindfors / multi.js

A user-friendly replacement for select boxes with the multiple attribute enabled
https://fabianlindfors.se/multijs
MIT License
953 stars 76 forks source link

How to reuse ? #11

Closed chenyb888 closed 7 years ago

chenyb888 commented 7 years ago

I'm using multi in a popup , after i close the popup and pop again, there's old value in multi. how to clean it?

fabianlindfors commented 7 years ago

Hi!

Multi always matches the select element it was initialized on. I would suggest you clear out the selection between uses of the popup and then trigger a change event on the select element to update multi.

Here's an example:

// Init multi.js with select_element
multi( select_element, {
    'enable_search': true,
    'search_placeholder': 'Search...',
});

// Do something else...

// Clear select_element selection
for ( var i = 0; i < select_element.options; i++ ) {
    select_element.options[i].selected = false;
}

// Create and trigger change event on element (to make sure multi updates)
var e = document.createEvent( 'HTMLEvents' );
e.initEvent( 'change', false, true );
select_element.dispatchEvent( e );
chenyb888 commented 7 years ago

thanks. but it's too complex. why do not implement a method just like .refresh()?

prototype-59 commented 7 years ago

with jquery you can use:

$( '#mylist" ).val([]);
$( '#mylist" ).trigger( "change" );

great plugin. .refresh function would be nice.

chenyb888 commented 7 years ago

ok,thank you