How to delete the first child of an element but referenced by $(this) in Jquery?

Posted by Raja on Stack Overflow See other posts from Stack Overflow or by Raja
Published on 2010-04-07T13:44:10Z Indexed on 2010/04/07 13:53 UTC
Read the original article Hit count: 156

Filed under:
|

The scenario is I have two Divs one is where I select items (divResults) and it goes to the next div (divSelectedContacts). When I select it I place a tick mark next to it. What I want to do is when I select it again I want to remove the tick mark and also remove the element from divSelectedContacts.

Here is the code.

    $("#divResults li").click(function() {
    if ($(this).find('span').size() == 1) {
        var copyElement = $(this).children().clone();
        $(this).children().prepend("<span class='ui-icon ui-icon-check checked' style='float:left'></span>");
        $("#divSelectedContacts").append(copyElement);
    }
    else {
        var deleteElement = $(this).find('span'); //here is the problem how to find the first span and delete it
        $(deleteElement).remove();
        var copyElement = $(this).children().clone();//get the child element
        $("#divSelectedContacts").find(copyElement).remove(); //remove that element by finding it
    }
});

I don't know how to select the first span in a li using $(this). Any help is much appreciated.

© Stack Overflow or respective owner

Related posts about jquery-selectors

Related posts about jQuery