Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda