Sean Magyar
Sean Magyar

Reputation: 2380

jquery selecting all the children classes with conditional

I would like to find all the .post-comment-replies that have '.post-comment-reply' nested in them (not first level).

With the code below I also get those .post-comment-replies that have no .post-comment-reply nested in them.

$(document).on('click', '.open-all-post-comments', function (event) {
  var post_id = $(this).data('pid');
  var all_replies = $('#post_' + post_id).find('.post-comment-replies');
  all_replies.show();
  $(this).closest('.open-all-post-comments-row').hide();
});

Upvotes: 1

Views: 44

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

You can do it with :has() selector,

var all_replies = $('#post_' + post_id)
                   .find('.post-comment-replies:has(.post-comment-reply)');

The :has() selector here will assist us to get the .post-comment-replies which has one or more descendant .post-comment-reply.

And if you want to make the immediate child to escape from the selector, then you can use,

var all_replies = $('#post_' + post_id)
                   .find('.post-comment-replies > :has(.post-comment-reply)');

Upvotes: 2

Related Questions