For the select menu plugin chosen.js, is there an established way to add 'select all items in list' or 'remove all items in list' feature to a multiple select input? In the main branch or perhaps in one of the forks? Or has someone done this before has some tips?
7 Answers
It should be pretty straight forward, just do it the "normal" way first:
$('.my-select-all').click(function(){
$('#my_select option').prop('selected', true); // Selects all options
});
Then trigger the liszt:updated event to update the chosen widget, so the whole thing would look something like this:
Update: For those who don't scroll down and check the other answers, the event is called chosen:updated as of a version released in August 2013. Consult the documentation if in doubt.
<select multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button class="select">Select all</button>
<button class="deselect">Deselect all</button>
$('select').chosen();
$('.select').click(function(){
$('option').prop('selected', true);
$('select').trigger('liszt:updated');
});
$('.deselect').click(function(){
$('option').prop('selected', false);
$('select').trigger('liszt:updated');
});
Working demo (js code is at the bottom): http://jsfiddle.net/C7LnL/1/
Tighter version: http://jsfiddle.net/C7LnL/2/
Even tighter version: http://jsfiddle.net/C7LnL/3/
- 1
- 1
- 101,186
- 37
- 194
- 228
-
well done @wesley - i figured it would be something along those lines. a great help! – Petrov Jun 23 '12 at 20:08
-
`trigger('liszt:updated')` is the key, even if you stop using chosen the code will still work. – Wesley Murch Jun 23 '12 at 20:15
-
when desselecting all select items i had a problem with updating the div display items , i solve it wit this : `$('div#mySelect_chzn ul.chzn-choices li.search-choice').remove();` – Salem Aug 27 '13 at 07:45
-
1Don't forget about e.preventDefault(); otherwise you will get effect of "flashing select and deselect" – Bartek S Oct 30 '13 at 07:50
-
3The event name is not 'liszt:updated' anymore, see Jack's answer bellow. – Paulo Scardine Nov 03 '13 at 22:52
-
3instead of $('select').trigger('liszt:updated'); $('select').trigger('chosen:updated'); worked for me. – Milan Maharjan Nov 26 '15 at 10:46
Try this code it will work 100%
// Deselect All
$('#my_select_box option:selected').removeAttr('selected');
$('#my_select_box').trigger('chosen:updated');
// Select All
$('#my_select_box option').prop('selected', true);
$('#my_select_box').trigger('chosen:updated');
- 4,496
- 2
- 23
- 24
-
I had to use ( ).prop('selected', false) for the deselect all function. – camainc Nov 16 '22 at 21:26
I missed the similar feature. This adds the two links All and None (texts customizable through options uncheck_all_text and select_all_text) on the top of the popup list. You may need to edit this if you use grouping.
$("select").on("chosen:showing_dropdown", function(evnt, params) {
var chosen = params.chosen,
$dropdown = $(chosen.dropdown),
$field = $(chosen.form_field);
if( !chosen.__customButtonsInitilized ) {
chosen.__customButtonsInitilized = true;
var contained = function( el ) {
var container = document.createElement("div");
container.appendChild(el);
return container;
}
var width = $dropdown.width();
var opts = chosen.options || {},
showBtnsTresshold = opts.disable_select_all_none_buttons_tresshold || 0;
optionsCount = $field.children().length,
selectAllText = opts.select_all_text || 'All',
selectNoneText = opts.uncheck_all_text || 'None';
if( chosen.is_multiple && optionsCount >= showBtnsTresshold ) {
var selectAllEl = document.createElement("a"),
selectAllElContainer = contained(selectAllEl),
selectNoneEl = document.createElement("a"),
selectNoneElContainer = contained(selectNoneEl);
selectAllEl.appendChild( document.createTextNode( selectAllText ) );
selectNoneEl.appendChild( document.createTextNode( selectNoneText ) );
$dropdown.prepend("<div class='ui-chosen-spcialbuttons-foot' style='clear:both;border-bottom: 1px solid black;'></div>");
$dropdown.prepend(selectNoneElContainer);
$dropdown.prepend(selectAllElContainer);
var $selectAllEl = $(selectAllEl),
$selectAllElContainer = $(selectAllElContainer),
$selectNoneEl = $(selectNoneEl),
$selectNoneElContainer = $(selectNoneElContainer);
var reservedSpacePerComp = (width - 25) / 2;
$selectNoneElContainer.addClass("ui-chosen-selectNoneBtnContainer")
.css("float", "right").css("padding", "5px 8px 5px 0px")
.css("max-width", reservedSpacePerComp+"px")
.css("max-height", "15px").css("overflow", "hidden");
$selectAllElContainer.addClass("ui-chosen-selectAllBtnContainer")
.css("float", "left").css("padding", "5px 5px 5px 7px")
.css("max-width", reservedSpacePerComp+"px")
.css("max-height", "15px").css("overflow", "hidden");
$selectAllEl.on("click", function(e) {
e.preventDefault();
$field.children().prop('selected', true);
$field.trigger('chosen:updated');
return false;
});
$selectNoneEl.on("click", function(e) {
e.preventDefault();
$field.children().prop('selected', false);
$field.trigger('chosen:updated');
return false;
});
}
}
});
I am also using a CSS to limit the height of the selected choises in case there are say tens of them:
.chosen-choices {
max-height: 150px;
}
- 81
- 1
- 1
-
1This seems the best option because it adapts to select. I really liked the result. – alex Jun 18 '14 at 19:53
Just straight forward way of clearing selection:
$('select').val('');
$('select').val('').trigger("chosen:updated");
- 5,223
- 7
- 42
- 71
-
1Close but you need to update the control. $('select').val('').trigger("chosen:updated"); – AntonK Jun 27 '16 at 07:03
I realize this question is quite old, but I came upon a similar issue and wanted to share my result, as I like it's simplicity:
I created two buttons (all and none) and floated them left and right inside the div containing my select dropdown. The buttons look something like this:
<button style="float:left;" class="btn" id="iAll">All</button>
<button style="float:right;" class="btn" id="iNone">None</button>
Then I added some Jquery to handle the button actions:
$('#iAll').on('click', function(e){
e.preventDefault();
$('#iSelect option').prop('selected', true).trigger('chosen:updated');
});
$('#iNone').on('click', function(e){
e.preventDefault();
$("#iSelect option:selected").removeAttr("selected").trigger('chosen:updated');
});
Will probably need some cleaning up with regards to layout, but it's quite functional as is and I thought I'd share.
- 156
- 7
$("#ctrlName option:selected").removeAttr("selected").trigger('liszt:updated');
clear all
$("#ctrlName option").attr("selected","selected").trigger('liszt:updated');
select all
- 61,146
- 24
- 125
- 222
You should try this:
$('#drpdwn').empty();
$('#drpdwn').trigger('chosen:updated');
- 1,196
- 1
- 12
- 30
- 3
- 3
-
This removes all elements from the dropdown and prevents any from being selected later. – Michael Sep 19 '14 at 22:47