Select2 deselect all values
Asked Answered
M

10

47

I want to deselect all values with one click without using each id seperately.

I fiddled around for a while, but this deselect only first value. Any suggestions?

This is how I try to deselect:

$( "#mybutton" ).click(function() {    
   $("select").select2('val', '')
});

http://jsfiddle.net/6hZFU/75/

Muscadel answered 13/9, 2013 at 8:57 Comment(1)
Please add code to a question that contains a fiddle.Lenoralenore
C
29

Try this instead:

$('select').select2({
    placeholder: "select me"
});

$("#mybutton").click(function () {
    $("select").each(function () { //added a each loop here
        $(this).select2('val', '')
    });
});

Demo here

Culley answered 13/9, 2013 at 9:0 Comment(2)
@danip, what version of select2 are you using, bunch of changes with 4.0, added a new answer belowFluting
@Fluting yes, I had problems with 4.0 but I still don't get why the each syntax works and multiple selector doesn'tPeriapt
J
73
$("select").val('').change();

That is all :) http://jsfiddle.net/6hZFU/78/

Explanation: when you change value of any input or select through JavaScript browser will not fire change event for that change (that is how specs say).

When you fire change event select2 will catch up and reflect new value of select.

Jotun answered 13/9, 2013 at 9:3 Comment(2)
Yes, because when you change value of any input or select through JavaScript browser will not fire change event for that change. When you fire change event select2 will catch up and reflect new value of select.Jotun
Your last comment should be added to your answer for clarification. Good explanation.Mephitic
C
29

Try this instead:

$('select').select2({
    placeholder: "select me"
});

$("#mybutton").click(function () {
    $("select").each(function () { //added a each loop here
        $(this).select2('val', '')
    });
});

Demo here

Culley answered 13/9, 2013 at 9:0 Comment(2)
@danip, what version of select2 are you using, bunch of changes with 4.0, added a new answer belowFluting
@Fluting yes, I had problems with 4.0 but I still don't get why the each syntax works and multiple selector doesn'tPeriapt
F
22

If using Select2 version 4.0, you will need to use the new syntax:

$("select").val(null).trigger("change");
Fluting answered 12/6, 2015 at 0:45 Comment(2)
This does not work on version 4, you have to enter empty string inside the val function. .val('').Farley
this is working in my select2 4.1 versionWatthour
D
8

In case someone is interested in the Select2 native configuration solution (no separate button).

As of time of writing (select2 v 4), Select2 can add a button to clear multiselection. No need for <option></option> workaround for placeholder to appear:

$('#my-select').select2({
    placeholder: "Select something",
    allowClear: true
});

From the documentation.

Diplegia answered 22/7, 2018 at 10:46 Comment(0)
L
3
$('#destinatariMail').val("-1").trigger("change");

This way you will deselect also tagged options that have been inserted "on-the-fly".

Llama answered 29/5, 2017 at 10:19 Comment(0)
W
2

I tried the above solutions but it didn't work for me.

This is kind of hack, where you do not have to trigger change.

$("select").select2('destroy').val("").select2();

or

$("select").each(function () { //added a each loop here
        $(this).select2('destroy').val("").select2();
});
Wilkins answered 12/4, 2017 at 10:43 Comment(1)
Exactly what I need! Thanks dude.Fleshly
F
1

If you want to clear multiple multiselect dropdown using Select2 v4 api, here is a simple way:

$("li.select2-selection__choice").remove();
$(".select2").each(function() { $(this).val([]); });

No focus on dropdown and clean both "select" and "Select2" elements ;)

Federal answered 23/6, 2015 at 18:32 Comment(0)
K
1

You need use the class of your select(select2_single)

Example:

$('#button_clear').click(function () 
{
    $('.select2_single').select2('val',''); 
});

Debes usar la clase de tus select

Ej:

$('#button_clear').click(function () 
{
    $('.select2_single').select2('val',''); 
});
Kin answered 4/4, 2017 at 16:34 Comment(4)
Please post answers in english. Thank youFideism
Could you provide your answer in english. ThanksFarrel
Ok, no problem, sorry for this problem!Kin
Ok robert, i try provide answers in inglish!Kin
D
1

try this

$('#mySelect2').val('');
$('#mySelect2').trigger('change.select2');
Darlington answered 6/12, 2023 at 6:57 Comment(0)
L
0

Try this, it's working perfectly.

$('#form-search-amenities > option').prop("selected", false);
$("li.select2-selection__choice").remove();

#form-search-amenities should e replace by ID of your <select> dropdown.

Loader answered 21/5, 2017 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.