Select2 deselect all values Select2 deselect all values javascript javascript

Select2 deselect all values


$("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.


Try this instead:

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

Demo here


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

$("select").val(null).trigger("change");