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

351
Visualizações
Django - i have add onclick function into html and whenever i click into different button then it will render the same data. how to fix that?
    <div class="home-food-row">
    {% if allmenu %} {% for menu in allmenu %}
    <div class="home-food-menu-col">
    <div class="filterDiv breakfast">
    <div class="img-price-tag">
    <div class="price-tag" id="price">{{menu.price}} only</div>
    <img class="home-page-food-img" src="{{menu.food_img.url}}" alt="Food" width="100%"/>
    </div>
    <h1 id="foodName_1">{{menu.food_name}}</h1>
    <p>{{menu.food_description}}</p>
    <button id="order" onclick="myFunction()">Place Order</button>
    </div>
    </div>
    {% endfor %} {% else %}
    <div class="col-md-12">
    <p>No Food</p>
    </div>
    {% endif %}
    </div>

/////////////////////////// JS Code ////////////////////////////

    function myFunction() {
    var price = document.getElementById("price");
    var food = document.getElementById("foodName_1");
    console.log(food);
    console.log(price);
   }

enter image description here enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Reason: all your menus have the same id. You can use the id of the menu to give each one a unique id, by appending {{ menu.id }} to the id attribute in your html.

    <div class="home-food-row">
    <strike> {% if allmenu %} {% for menu in allmenu %} <strike>
    <div class="home-food-menu-col">
    <div class="filterDiv breakfast">
    <div class="img-price-tag">
    <div class="price-tag" id="price{{ menu.id }}">{{menu.price}} only</div>
    <img class="home-page-food-img" src="{{menu.food_img.url}}" alt="Food" width="100%"/>
    </div>
    <h1 id="foodName_1{{ menu.id }}">{{menu.food_name}}</h1>
    <p>{{menu.food_description}}</p>
    <button id="order" onclick="myFunction('{{ menu.id }}')">Place Order</button>
    </div>
    </div>
    <strike>{% endfor %} {% else %}<strike>
    <div class="col-md-12">
    <p>No Food</p>
    </div>
    <strike>  {% endif %}<strike>
    </div>

Then you can target these individually in your javascript like this:

    function myFunction(id) {
    var price = document.getElementById("price" + id);
    var food = document.getElementById("foodName_1" + id);
    console.log(food);
    console.log(price);
   }
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