Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just wrote this code but I can't find a way to make it cleaner:

$(".graphic").click(function() {
  $(".graphic i").toggleClass("fa-minus-square");
});

$(".web").click(function() {
  $(".web i").toggleClass("fa-minus-square");
});

HTML:

<button class="btn graphic"><i class="fa fa-plus-square" aria-hidden="true"></i>Graphic designer</button>
<button class="btn web"><i class="fa fa-plus-square" aria-hidden="true"></i>Graphic designer</button>

I wanted something like the code below, but preventing those buttons from changing at the same time:

$(".btn").click(function() {
  $(".btn i").toggleClass("fa-minus-square");
});
share|improve this question

closed as unclear what you're asking by forsvarir, t3chb0t, ferada, Graipher, mdfst13 Oct 4 at 12:16

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Use $(this) inside the click callback. $(this).find('i').toggleClass('fa-minus-square'); or $('i', this).toggleClass('fa-minus-square'); – Tushar Oct 4 at 3:27
    
Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it. – Tushar Oct 4 at 3:28
up vote 1 down vote accepted

Use one handler for both and $(selector, this) to find selector inside the clicked button:

$(".graphic, .web").click(function() {
  $("i", this).toggleClass("fa-minus-square");
});
share|improve this answer
    
Thank you @wOxxOm! – Felipe Felix de Luca Oct 5 at 3:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.