-1

Is there a way to clear all items in a dropdown using jquery ? I will have to clear all items before i call the following method to avoid duplication

   $('#drpeventproducts').append($('<option>', {
                        value: data.id,
                        text: itemname
                    }));
logeeks
  • 4,849
  • 15
  • 62
  • 93
  • Does this answer your question? [How do I clear the dropdownlist values on button click event using jQuery?](https://stackoverflow.com/questions/2985072/how-do-i-clear-the-dropdownlist-values-on-button-click-event-using-jquery) – costaparas Jan 18 '21 at 06:42

2 Answers2

1

You can just do below. That will remove all the options under the select with id "drpeventproducts"

$('#drpeventproducts').html('');

or

$('#drpeventproducts').empty();

$('#drpeventproducts').html('');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="drpeventproducts"><option value="1">1</option></select>
A Paul
  • 8,113
  • 3
  • 31
  • 61
0

Refer this link

$('#drpeventproducts')
        .find('option')
        .remove()
        .end()
        .append($('<option>', {
              value: data.id,
              text: itemname
        }));
Malika
  • 7
  • 5