简体   繁体   中英

Remove item from array using jQuery button

I have a list, which has an image, and an 'x' image (Close.gif) I am not sure how to make the ListItem be removed when the 'x' image is clicked.

<ul id="cardList" class="cardList">    
    <li id="1" class="ui-state-default" data-type="cover" onclick="onClickCard(this)">
    Cover 00 
    <img src="./Close.gif" alt="" onclick="onClickClose(this)"/>
    </li>
</ul>

I only know how to have the image removed when the 'x' mark is clicked.

var onClickCard = function(asd){
    $(asd).remove();
}

I assume that your onClickCard function is used to remove list items when you click on the list item itself and the function you want to handle here is onClickClose

After that, to solve your problem you can use .parent() or .closest() to remove parent li of clicked image:

var onClickClose = function(asd){
    $(asd).parent().remove(); // or .closest('li').remove()
}

First of all, you've got the name of the function wrong if you want it to fire when you click the image.

Change your function to this...

var onClickClose = function(asd){
    $(asd).closest("li").remove();
}

You can try with closest()

var onClickClose= function(asd){
    $(asd).closest("li").remove();
}

try this:

var onClickClose = function(asd){
   $(asd).closest('li').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