Search code examples
jqueryjquery-selectors

Check if element contains another one in jQuery


I found many related things on Stack Overflow but not applicable for my case.

It's all in this exemple, I need to check if an element contains another one, and if yes, append something.

$(".bt_repondre").click(function(){
    comment = $(this).parent().parent().parent();
    //I want to check if comment contains a .comment_full element, if no, append.
    comment.append('add');
});

Hope you can help me, I tried so many things...


Solution

  • Just use .find() and check if returns an element, like this:

    $(".bt_repondre").click(function(){
        comment = $(this).parent().parent().parent();
        if (! comment.find('.comment_full').length) {
           comment.append('add');
        }
    });