Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/

like image 605
Jaanus Avatar asked Sep 13 '13 08:09

Jaanus


People also ask

How to unselect all options in select2?

$('#form-search-amenities > option'). prop("selected", false); $("li. select2-selection__choice"). remove();

How do I make select2 empty?

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();

How to select option in select2?

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).

How to unselect all options jQuery?

Unselect all Values using “element” as a Selector$('#accessories'). val(''). trigger("change");


1 Answers

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

like image 108
Ivan Ivanic Avatar answered Sep 21 '22 01:09

Ivan Ivanic