I had the same problem today, as a rule, it is advised to use a list of hidden flags and emulate behavior using css, thus it is easier to manage, but in my case I do not want to change the html.
At the moment, I tested this code only with google chrome, I donβt know if it works with another browser, but it should:
var changed; $('select[multiple="multiple"]').change(function(e) { var select = $(this); var list = select.data('prevstate'); var val = select.val(); if (list == null) { list = val; } else if (val.length == 1) { val = val.pop(); var pos = list.indexOf(val); if (pos == -1) list.push(val); else list.splice(pos, 1); } else { list = val; } select.val(list); select.data('prevstate', list); changed = true; }).find('option').click(function() { if (!changed){ $(this).parent().change(); } changed = false; });
Of course, suggestions are welcome, but I have not found another way
Marino Di Clemente 01 Oct '14 at 18:22 2014-10-01 18:22
source share