You need to use if
for confirm then .closest('li')
to get the closest li
then use .remove()
Don't use attributes like removecartid =
you can use data
attribute instead data-removecartid
and catch it by .data('removecartid')
or .attr('data-removecartid')
.. jQuery Data vs Attr?
$(document).on('click' , '#delcarts > li .removecart' , function () {
var pid = $(this).attr('data-removecartid');
var conf = confirm("Are you sure you want to delete item? "+pid);
if(conf){
$(this).closest('li').remove();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="delcarts">
<li class="box-li-cart">
<div class="col-xs-3 col-md-3 box-cart-book"><img src="'.$book_image.'"/></div>
<div class="col-xs-8 col-md-8 minicart-data">
<p class="mc-name">Book title</p>
<p>Qty && Book Price</p>
</div>
<div data-removecartid="someid" id="removecart" class="btn-remove-prod removecart"><a ><i class="fa fa-times" aria-hidden="true">Remove</i></a></div>
</li>
</ul>
If you dynamically generate the li
s you will need Event binding on dynamically created elements?
$(document).on('click' , '#delcarts > li .removecart' , function () {
If you're trying to use onclick=
function removecart(el) { //el here
var pid = $(el).closest('.removecart').attr('data-removecartid'); // el here
var conf = confirm("Are you sure you want to delete item? "+pid);
if(conf){
$(this).closest('li').remove();
}
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="delcarts">
<li class="box-li-cart">
<div class="col-xs-3 col-md-3 box-cart-book"><img src="'.$book_image.'"/></div>
<div class="col-xs-8 col-md-8 minicart-data">
<p class="mc-name">Book title</p>
<p>Qty && Book Price</p>
</div>
<div data-removecartid="SomeId_Here" id="removecart" class="btn-remove-prod removecart">
<a>
<i class="fa fa-times" aria-hidden="true" onclick="removecart(this)">Remove</i>
</a>
</div>
</li>
</ul>
Note: By using onclick=removecart(this)
in <i>
icon or
its parent <a>
it will remain the same action