Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
121
rated 0 times [  127] [ 6]  / answers: 1 / hits: 19140  / 7 Years ago, tue, january 23, 2018, 12:00:00

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/


More From » jquery

 Answers
11

You can just do:



$(#workload-selector).select2('val', '');


Also according to the docs this also works:



$(#workload-selector).val();
$(#workload-selector).trigger(change);

[#55392] Friday, January 19, 2018, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
carrington

Total Points: 674
Total Questions: 90
Total Answers: 108

Location: Burundi
Member since Sat, Aug 21, 2021
3 Years ago
;