addClass inside this

Asked

Viewed 29 times

1

I have this HTML:

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

In Jquery when you click the button, I add a class to it like this:

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

How do I add a class in the ARROW class? I tried this, but it wasn’t:

$(this '.seta').addClass('seta_ativa');

1 answer

2


If .seta is descended from this you can do it like 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>
<div class="botao">
  <div class="seta">Clique aqui</div>
</div>

In the general case it would be:

$('.seta').addClass('seta_ativa');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.