removing all options of select box except 1st option

Jquery

Jquery Problem Overview


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();

Jquery Solutions


Solution 1 - Jquery

Try this

yourSelect.find('option').not(':first').remove();

Solution 2 - Jquery

I was looking for one line code, what I found after lots of research is the Simplest and Best one line code to remove everything except first.

JQuery,

$('#ddlId option:not(:first)').remove();

Solution 3 - Jquery

you can use the greater than :gt() selector of jQuery for this

$('#lForm select[id!="makeClick"] select[id!="stateClick"] select[id!="cityClick"] option:gt(0)').remove().end();

This will remove all options that are greater than 0

Solution 4 - Jquery

You can use not(:first)

$('#lForm select[id!="makeClick"] select[id!="stateClick"] select[id!="cityClick"]  option:not(:first)').remove().end();

Solution 5 - Jquery

The easiest way to clear the options in the way you're describing is by using options.length = 1. Also, you can leverage the fact that each drop down clears the ones that logically follow it, so that you only need to declare a single change handler.

$('#lForm select').on('change', function() {
  if (this.selectedIndex > 0) {
    var $others = $(this).closest('table').find('select'),
    current = $others.index(this); // find current

    while (++current < $others.length) {
      // for each following drop down
      $others.get(current).options.length = 1;
    }
  }
});

Demo

I'm not sure how you're going to repopulate the drop downs though :)

Solution 6 - Jquery

It's Work

$('#mClick').change(function () {
    $('#sClick, #cClick, #srClick').find("option:gt(0)").remove();
});
$('#sClick').change(function () {
    $('#cClick, #srClick').find("option:gt(0)").remove();
});
$('#cClick').change(function () {
    $('#srClick').find("option:gt(0)").remove();
});

Solution 7 - Jquery

asssign general class to your Dropdown then

$('.general option[value!=""]').remove();

this will remove all options except first one whose value is empty Hope this will helps

Solution 8 - Jquery

Try this

$('#lForm select').change(function(){
   var $selects = $('#lForm select');
   var idx = $selects.index(this);

   // Use `val` function to clear the selected values or Use `remove` function to remove the element.
   $selects.filter(':gt(' + idx +')').val('');

});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsraklView Question on Stackoverflow
Solution 1 - JqueryDawid KuśmierekView Answer on Stackoverflow
Solution 2 - JquerySudi065View Answer on Stackoverflow
Solution 3 - JqueryU.PView Answer on Stackoverflow
Solution 4 - JquerySatpalView Answer on Stackoverflow
Solution 5 - JqueryJa͢ckView Answer on Stackoverflow
Solution 6 - JqueryRenish PatelView Answer on Stackoverflow
Solution 7 - JqueryKhanView Answer on Stackoverflow
Solution 8 - JqueryM.G.ManikandanView Answer on Stackoverflow