Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing all options of select box except 1st option

Tags:

jquery

I'm trying to empty select options when:

id "mClick" is selected, id's "sClick", "cClick" and "srClick" will be emptied.

id "sClick" is selected, id's "cClick" and "srClick" will be emptied.

id "cClick" is selected, id "srClick" will be emptied.

<form action="javascript:void(0);" method="POST" id="lForm"> <table>     <tr>         <td>             <select name="module" id="mClick">                 <option value="">Select Mod</option>                 <option value="1">Mod 1</option>                 <option value="2">Mod 2</option>                 <option value="3">Mod 3</option>             </select>         </td>         <td>             <select name="state" id="sClick">                 <option value="">Select State</option>                 <option value="1">State 1</option>                 <option value="2">State 2</option>             </select>         </td>         <td>             <select name="city" id="cClick">                 <option value="">Select City</option>                 <option value="1">City 1</option>                 <option value="2">City 2</option>             </select>         </td>         <td>             <select name="services" id="srClick">                 <option value="">Select Services</option>                 <option value="1">Services 1</option>                 <option value="2">Services 2</option>             </select>         </td>     </tr> </table> 

in scenario 3, i used this function, but it deleted all, except the last select. Any idea's what i'm missing? Thanks

$('#lForm select[id!="mClick"] select[id!="sClick"] select[id!="cClick"] option[value!=""]').remove().end(); 
like image 412
srakl Avatar asked Jul 09 '13 07:07

srakl


People also ask

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

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

How do I remove all select options?

To remove all options from a select list using jQuery, the simplest way is to use the empty() method. If we want to remove all options from the select list, we can use the jQuery empty() method to do this with the following Javascript code.

How do I get the first option selected in jQuery?

Select the <select> element using JQuery selector. This selector is more specific and selecting the first element using option:nth-child(1). This will get access to the first element (Index starts with 1).


1 Answers

Try this

yourSelect.find('option').not(':first').remove();
like image 112
Dawid Kuśmierek Avatar answered Sep 20 '22 20:09

Dawid Kuśmierek