简体   繁体   中英

removing item from list (JQuery)

i have a table with car models in it. When a user clicks a car the selected car is shown in a list under the table. Now, i wanna so when the user clicks the car again it is removed from the list.

jsfiddle demo

Have you tried using remove() ?

//DONNO WHAT TO PUT
$(this).remove();

UPDATE

Considering a non-dynamic table structure you can use index() to safely pinpoint a row, uniquely. Combining it with an id (preferably starting with a unique string, as not to clash with the rest of your page, like list_ ) and remove(), you should get the required effect.

Here's a fork of your code .

Here you go!

I added a ref to the link you are adding to the row element, thus you can check for it and remove it:

function colorStay(){
    $(this).css('background-color','red');
    $(this).unbind('mouseover', changeColor);
    $(this).unbind('mouseout', removeColor);

    $('#stay').css({
        paddingTop:15,
        fontSize:22
    });
    var staylink = $("<a id='"+ $(this).find(':last').text() +"' href='http://www.google.ca'>"+ $(this).find(':last').text() +"</a><br />");
    $('#stay').append(staylink);
    $(this)[0].staylink = staylink;

}

function remove(){
    $(this).css('background-color','white');
    $(this).bind('mouseover', changeColor);
    $(this).bind('mouseout', removeColor);

    //DONNO WHAT TO PUT
    if($(this)[0].staylink){
        $(this)[0].staylink.remove();
        $(this)[0].staylink = false;

    }
}

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