Search first Element inside another Element if it exists in jQuery

1

I have a problem finding an Element inside Another and checking if it is the first element, see the structure:

<div class="form-group">
  <input type="text" id="teste1" placeholder="Texto">
  <label for="teste1">Texto</label>
  <p class="help-block">Teste.</p>
</div>
<div class="form-group">
  <label>Texto</label>
  <p>Texto</p>
  <p class="help-block">Teste.</p>
</div>

I want to select the tag label within form-group but only if it is the first element, I tried this way in jQuery :

if ( $('.form-group').children().first().is('label') ) {
  $(this).parent().css("padding-top", "0");
}

But the answer is false .

    
asked by anonymous 07.09.2016 / 15:03

1 answer

2

$('.form-group') returns an array containing all elements of the class.

$('.form-group').each(function() {
    var primeiro = $(this).children().first();
    if($(primeiro).is('label')) {
    $(primeiro).parent().css('padding-top', '0');
  }
});

example: link

    
07.09.2016 / 17:41