Jean-François G. B. Jean-François G. B. - 5 months ago 10
jQuery Question

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 containa a .comment_full element, if no, append.
comment.append('add');
});


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

Answer

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');
    }
});