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/
$('#form-search-amenities > option'). prop("selected", false); $("li. select2-selection__choice"). remove();
In order to clear the selection of those values which are selected using a Select2 drop down,we can use the empty() function. The dropdown can be reset using Jquery. $("#select2_example"). empty();
New options can be added to a Select2 control programmatically by creating a new Javascript Option object and appending it to the control: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data. text, data.id, false, false); $('#mySelect2'). append(newOption).
Unselect all Values using “element” as a Selector$('#accessories'). val(''). trigger("change");
$("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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With