How do I get a single div with the same class in jquery?

1

How can I use the click function in jquery with classes of the same name? Type click and appear element only in the clicked div and not in the others. I've already tried using $(this).next('bla').addClass('blablabla')

and I was not successful.

    
asked by anonymous 01.04.2018 / 06:36

1 answer

2

Simple use the find method:

$(document).ready(function() {
  $('.lista').click(function() {
    $(this).find(".item").toggleClass('green');
  });
});
.lista {
  border-bottom: 1px solid #ccc;
  cursor: pointer;
  margin-bottom: 14px;
  padding-bottom: 14px;
}
.item {
}

.green { color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="lista">Clique para mudar a cor do item:<div class="item">pt.stackoverflow.com</div>
</div>
<div class="lista">Clique para mudar a cor do item:<div class="item">pt.stackoverflow.com</div>
</div>
    
01.04.2018 / 08:28