addClass within this

1

I have this HTML:

<div class="botao">
   <div class="seta"></div>
</div>

In Jquery when the button is clicked, I add a class in it like this:

$(this).addClass('botao_ativo');

How do I add a class in the SETA class? I tried this, but it was not:

$(this '.seta').addClass('seta_ativa');
    
asked by anonymous 06.07.2017 / 19:02

1 answer

2

If .seta is descending from this you can do this:

$(this).find('.seta').addClass('seta_ativa');
// ou alternativamente:
$('.seta', this).addClass('seta_ativa');

Example:

$('.botao').on('click', function() {
  $('.seta', this).addClass('seta_ativa');
});
.seta_ativa {
  background-color: #eaa;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="botao">
  <div class="seta">Clique aqui</div>
</div>

In the general case it would be:

$('.seta').addClass('seta_ativa');
    
06.07.2017 / 19:06