Search code examples
javascriptjqueryhtmlselectionjquery-select2

Select2 deselect all values


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/


Solution

  • Try this instead:

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

    Demo here