Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unselect options in select using jquery

Below is my HTML. I have given multiple option elements in the select tag.

<select class="car" multiple size="3">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
</select>

I am able to multi-select by holding the CTRL key. I am able to retrieve the selected values using the following jQuery

$.each($(".car option:selected"), function() {
    countries.push($(this).val());
});

How can I unselect the value using jQuery? The selected value will be highlighted as shown:

enter image description here

Thanks in advance

like image 403
Akhil Ghosh Avatar asked Aug 31 '16 09:08

Akhil Ghosh


People also ask

How do I unselect a selection?

Pressing the Ctrl key, you can click, or click-and-drag to deselect any cells or ranges within a selection. If you need to reselect any of those cells, continue holding the Ctrl key and reselect those cells (for Mac, use the Cmd key).

How do I remove dynamically selected options in jQuery?

Approach: Select the option from select which needs to remove. Use JQuery remove() method to remove the option from the HTML document.

How do you remove all options from select in jQuery except first?

If we want to remove all items from dropdown except the first item then we can use $('#ddlItems option:not(:first)'). remove(); Here we have excluded first item from being deleted. If we want to remove all items from dropdown except the last item then we can use $('#ddlItems option:not(:last)').


2 Answers

Set the selected property to false: $(selector).prop('selected', false).

I have added a button and attached a click event to be able to demonstrate.

var countries = [];

function unselect() {
    $.each($(".car option:selected"), function () {
        countries.push($(this).val());
        $(this).prop('selected', false); // <-- HERE
    });
}

$("#unselect").click(unselect);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select class="car" multiple size="3">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
</select>

<input type="button" value="unselect" id="unselect" />
like image 191
beerwin Avatar answered Sep 21 '22 06:09

beerwin


You can use $('.car').val([]); for unselect all options in multi-select dropdown.

For multi select value you can pass empty array for unselect all options.

$(document).ready(function(){
  $("#select").click(function(){
    var values= [];
    $(".car > option").each(function(){
    	values.push($(this).attr('value'));
    });
    $('.car').val(values);
  });
  
  $("#unselect").click(function(){
    $('.car').val([]);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
 <select class="car" multiple size="3">
  <option value="volvo" selected>Volvo</option>
  <option value="saab" selected>Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
  </select>
  <button id="select">Select All</button>
  <button id="unselect">Unselect All</button>
  </button>
like image 42
Haresh Vidja Avatar answered Sep 24 '22 06:09

Haresh Vidja