Kanishka Kanishka - 8 months ago 37
Javascript Question

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>

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download