i have two lists one for products to select and the other for the selected products i want to be able to return the selected product to its original place.
I kinda solve it in the following format but the issue is it might be hundreds of products
$(document).on('click', "#remove_product", function () {
var product = $(this).parents("li"),
found = false;
$('.products_list li').each(function() {
if(this.id > product.attr('id') && !found ){
product.clone().insertBefore(this);
found = true;
}
});
if(!found) product.clone().append(".products_list");
product.remove();
})
.products_list button{
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="products_list">
<li id="1">Content 1</li>
<li id="2">Content 2</li>
<li id="4">Content 4</li>
<li id="5">Content 5</li>
</ul>
<hr>
<ul class="selected_products">
<li id="3">
Content 3
<button id="remove_product">remove product</button>
</li>
</ul>
Any better solution?