[Answer]-Django Templates + jQuery + better way to do

1👍

Dont use id for elements that occur more than once. That’s probably why only your first one works and not the others. Same issue with id="answer" – use a class, and find it via jQuery.

{% for photo in pops.photos.all %}
        <div class="thumbnail span3" id="pop_picture">
          <img src="{{MEDIA_URL}}{{photo.original_image}}"alt="{{photo.name}}">
          <br>
          <p class="answer">{{photo.name}}</p>
          <input class="txt_pop" type="text"  value=""/>
          <button class="btn pops_button" type="submit">confere</button>
        </div>
{% endfor %}

$(document).ready(function() {
    $('#pops .pops_button').click(function() {
       alert($(this).parent().find('.answer').html());
    }); 


});

Leave a comment