Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear multiple select jquery-select2

I am trying to create a reset button for jquery-select2 multiple select. I have no idea why my solution doesn't work.

Html:

<select id="workload-selector" class="js-source-states-2" multiple="multiple" style="width: 100%">             
  <option value="haha">haha</option>
  <option value="haha2">haha2</option>
  <option value="haha3">haha3</option>
</select>

<button id="reset">
Reset
</button>

Javascript:

$("#workload-selector").select2();
$("#reset").click(function(){
  $("#workload-selector option:selected").removeAttr("selected");
});

I made it on jsFiddle: https://jsfiddle.net/DTcHh/41975/

like image 596
Dracke Avatar asked Dec 02 '22 10:12

Dracke


1 Answers

The select2 multiple saves value in an array All you need to do is

$("#workload-selector").val([]).change();
like image 82
Murtaza Mehmudji Avatar answered Dec 04 '22 07:12

Murtaza Mehmudji