简体   繁体   中英

Remove item from Div Jquery

I have a HTML code and i need to remove the item from div.

HTML

    <div class="dropdown-menu dropdown-menu-right" id="popcart">
<div class="media" id="2">
<a href="shop-single.html">
<span class="qty">1</span> x <span class="price">1790.00</span><button type="button" class="close" aria-label="Close" onclick="btnpopitemclose(2)">
<div class="media" id="5">
<a href="shop-single.html">
<span class="qty">1</span> x <span class="price">1990.00</span><button type="button" class="close" aria-label="Close" onclick="btnpopitemclose(5)">
</div>
</div> 

I have tried it like below Jquery code.But it's not success

   function btnpopitemclose(ItemId) {

    $(this).closest('<div>').remove();//not success
$(this).parent('<div>').remove();//not success
$(this).remove();//not success
}

Use a jQuery Selector:

A string containing a selector expression to match elements against.

See more: https://api.jquery.com/closest/

Example

 $(function() { $("button.close").click(function(e) { e.preventDefault(); var trg = $(this).closest("div"); console.log("Removing " + trg.attr("id")); trg.remove(); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="dropdown-menu dropdown-menu-right" id="popcart"> <div class="media" id="2"> <a href="shop-single.html"> <span class="qty">1</span> x <span class="price">1790.00</span> </a> <button type="button" class="close" aria-label="Close"> x </button> </div> <div class="media" id="5"> <a href="shop-single.html"> <span class="qty">1</span> x <span class="price">1990.00</span> </a> <button type="button" class="close" aria-label="Close"> x </button> </div> </div> 

Hope that helps.

give id to parent attribute of the attribute that you want to remove here i have used id "main_div" i am removing 2nd child of parent div

you can write $("#main_div").child().next().remove();

hope it works for you

Hope this is work for your dropdown change event

If it's a button click then replace $('#popcart').off().on('change', by $('#popcart').off().on('click', ie click event

$('#popcart').off().on('change',function(){
var $this=$(this);
$this.closest('div[class="media"]').remove();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM