luca
luca

Reputation: 37136

jquery loop to check if element is empty

<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

Upvotes: 2

Views: 3968

Answers (3)

chriscauley
chriscauley

Reputation: 21001

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.

Upvotes: 0

Alpesh
Alpesh

Reputation: 5405

Try this -

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

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


});

Upvotes: 2

js1568
js1568

Reputation: 7032

$('div.child_2:empty').each(function() {
  $(this).prev('div.child_1').empty();
});

Upvotes: 8

Related Questions