On my web page, I have listed some data in an input select dropdown using jQuery. Before append data, I want to remove options from the select field after the first child. How can I do it?
My code:
<script> if(result.data.length > 0) { for(i=0; i<result.data.length; i++){ $("#itemData").append('<option value="'+result.data[i]['id']+'">'+result.data[i]['name']+'</option>'); } } </script>
Add this code before append data.
$('#itemData').find('option:not(:first)').remove();
For example:
<script> if(result.data.length > 0) { $('#itemData').find('option:not(:first)').remove(); for(i=0; i<result.data.length; i++){ $("#item_category").append('<option value="'+result.data[i]['id']+'">'+result.data[i]['name']+'</option>'); } } </script>
Question and answer is powered by AnsPress.io