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

185
Views
Toggle text on click on a query loop

I'm trying to make a section that lists content on a query and ends with a link that changes it's name upon click. In this context, 'objetos' displays data from a class that I then turn to html.

{% for i in objetos %}
...
<a onclick="change()" id="{{i.id}}">Fav</a>
    <script>
        function change() {
            var elem = document.getElementById("{{i.id}}");
            if (elem.innerHTML == "Fav")
                elem.innerHTML = "Unfav";
            else
                elem.innerHTML = 'Fav';
            }
    </script>
...
{% endfor %}

However the script seems to only load the last value of 'i', and so only the last link changes. Do I have to save it every time?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Replace your code with this.

{% for i in objetos %}
...
<a onclick="change(this)" id="{{i.id}}">Fav</a>
...
{% endfor %}

<script>
    function change(e) {
        var elem = document.getElementById(e.getAttribute('id'))
        if (elem.innerHTML == "Fav")
            elem.innerHTML = "Unfav";
        else
            elem.innerHTML = 'Fav';
        }
</script>

Your actual rendered code is this.

...
<a onclick="change()" id="1">Fav</a>
    <script>
        function change() {
            var elem = document.getElementById("1");
            if (elem.innerHTML == "Fav")
                elem.innerHTML = "Unfav";
            else
                elem.innerHTML = 'Fav';
            }
    </script>
...

...
<a onclick="change()" id="2">Fav</a>
    <script>
        function change() {
            var elem = document.getElementById("2");
            if (elem.innerHTML == "Fav")
                elem.innerHTML = "Unfav";
            else
                elem.innerHTML = 'Fav';
            }
    </script>
...

...
<a onclick="change()" id="3">Fav</a>
    <script>
        function change() {
            var elem = document.getElementById("3");
            if (elem.innerHTML == "Fav")
                elem.innerHTML = "Unfav";
            else
                elem.innerHTML = 'Fav';
            }
    </script>
...

Inspect on browser. You will see this.

So when you click on first the last gets called as it matches the js function name. If you create multiple js function name with same name, last function would be use It's same as reassgning value Same way in your code the last function was getting called always. Hence was the issue.

about 4 years ago · Juan Pablo Isaza Report
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!