I have the following code snippet:
<ul class="ul" id="selected_conditions"> <li data-field="asset_locations_name" data-condition="in"> <i class="fa fa-minus-circle delete_condition" aria-hidden="true" title="Click to remove this condition from the list"></i> WHERE asset_locations_name IN( <span class="condition_item" data-id="1213381233"> <i class="fa fa-minus-circle delete" title="Click to remove this item from the list" aria-hidden="true"></i> 1213381233 </span>, <span class="condition_item" data-id="1212371897"> <i class="fa fa-minus-circle delete" title="Click to remove this item from the list" aria-hidden="true"></i> 1212371897 </span> ) </li> </ul>
Every time I click on the small .delete icon, I have to delete the current value, and I was able to achieve this with the following code:
$(function() { $('#selected_conditions').on('click', '.delete', function(ev) { var item_id = $(this).parent().data('id'); $('.condition_item[data-id="'+ item_id +'"]').remove(); }); });
But there are problems in the above code: if I delete all the elements, we end up with the following () empty string, and I cannot have it:
- How to count SPAN inside parenthesis
() and raise a warning if I click on the last?
Note. The correct behavior will display a confirmation dialog to the user and if the user clicks "OK" and agrees to delete the last item, all state should be deleted. The value, if I click on the last item, should be deleted> the entire parent li .
Any help? I leave you a fiddle to play. This is WIP, so if you have a suggestion or a better solution, feel free to add it to your answer.
source share