Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
targeting id on a loop to toggleClass with Jquery

I'm trying to make it so when a user presses a button, an element inside the button changes class. I managed to do that when I have a clear id for button.

<button id="buttonId"><i class="class1"></i></button>
<script>
    $(document).ready(function(){
        $("#buttonId").click(function(){
            $(this).find('i').toggleClass('class2').toggleClass('class1');
        });
    });
</script>

Now I need to create a sequence of buttons, and for that I've set the id like this:

{% for i in X %}
<button id="{{i.id}}"><i class="class1"></i></button>
{% endfor %}

However I don't know how I can reference i.id inside the function. The example below didn't work.

$(document).ready(function(){
    $("{{i.id}}").click(function(){
        $(this).find('i').toggleClass('class2').toggleClass('class1');
    });
});

How do I find the id?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!