Search code examples
javascriptjqueryhtmljquery-ui-multiselect

Remove items multi select with optgroup in jquery


Is there a way to clear all option and optgroup HTML elements by using jquery?

My HTML select element is like this,

<select id="users" name="multiselect" multiple="multiple" style="width:382px;" >
    <optgroup id="groupadmin" label="Group Admin"></optgroup>
    <optgroup id="systemusers" label="System User"></optgroup>
</select>

Solution

  • You can use .empty()

    Description: Remove all child nodes of the set of matched elements from the DOM.

    Code

    $('#users').empty()
    

    Fiddle

    EDIT

    As you are using multiSelect plugin, You need to use .multiSelect('refresh') method.

    $('#users').empty().multiSelect('refresh'); 
    

    Updated Fiddle