2
<div class="parent">

 <div class="child_1">

  content to remove if child_2 is empty!

 </div>

 <div class="child_2">

  content of child_2

 </div>

in the page repeated a dozen times..how could i check if any "child_2" element is empty and if so remove content of "child_1"??

thanks

Luca

3 Answers 3

8
$('div.child_2:empty').each(function() {
  $(this).prev('div.child_1').empty();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Beware :empty is only true for "no children whatsoever", not for elements with "whitespace-only" content.
2

Try this -

$('.child_2').each(function(){

  if($(this).html()==''){
    $(this).prev('.child_1').html('');
  }


});

Comments

0

Are you familiar with $.each()?

$(".child_1").each(function() {
    var child_2 = $(this).siblings(".child_2"); // "this" is the element being iterated over
    if (child_2.html().length == 0) {
        $(this).html("");
    }
}

I think that is what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.