I’m working on a multiselect that creates an <li> using the text of each :selected option. How would I go about removing that <li> if the option becomes unselected?
I’m using the code below (from here) to append the <li> for each :selected item.
$('option:selected', select).each(function(index, value) {
$('ul.multiselect-dropdown-options-'+name+'').append('<li>'+$(value).text()+'</li>');
});
I’ve adapted your code to the following:
JS Fiddle demo
This takes care of the de-selection by, each time the
selectchanges, emptying theuland then appending only those elements that are selected into theul.The
lielements are, however, given adata-*attribute containing a reference to the original value of theoptionfrom which they were taken/copied, so it should be possible to relate thelito the originaloptionshould you want to take a different approach.References:
change().empty().