I am think this should be fairly easy, but struggling a bit ... I have a SELECT
input element that allows the user to select multiple items. I would like to provide a mechanism for them to clear all selected items, probably will just be a SPAN
tag or something similar.
Anyway ... using jQuery, how I would I clear the selection on the SELECT
input element so no items are selected.
Approach: Select the option from select which needs to remove. Use JQuery remove() method to remove the option from the HTML document.
You can deselect any cells within the selected range with the Deselect Tool. 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).
Explanation: 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)').
To remove the options of an HTML element of select , you can utilize the remove() method: function removeOptions(selectElement) { var i, L = selectElement.
In the case of a <select multiple>
the .val()
function takes/returns an array, so you can simply pass in an empty array to clear the selection, like this:
$("#selectID").val([]);
You can test it out here.
This worked to clear all selected options for me..
$("#selectListName").prop('selectedIndex', -1)
The select list looked like
<select multiple='multiple' id='selectListName'> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </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