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

Asked

Viewed 724 times

1

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

and did not succeed.

  • I did not understand your question very well, you want that even if you have equal classes, it is inserted only in the one that was clicked?

  • Put the code in the question. This way is not clear.

  • I want to click on a class that has the same name as the others, only to add a class just clicked on.

  • @darknone already tried to use an id?

  • It hasn’t worked either

  • I’ve solved it, I just used siblings :)

  • 1

    I could post your code to see how it turned out!

Show 2 more comments

1 answer

2


Simple use the method find:

$(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>
<div class="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>

Browser other questions tagged

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