Jquery only works on the first element of forloop?

advertisements

Here is my Django template:

{% for feed in feeds %}
    <div id="feed"><b>At {{feed.location}}:<b> {{feed.msg}}</b></div></br>
    <button id="bb{{feed.id}}">Add Comment</button>
    <div id="commentbox" style="display:none;">
      <form method="post" action="/comment/{{feed.id}}/">
        {{cform.as_p}}
        <input type="submit" value="comment" />
      </form>
    </div>

{% endfor %}

Jquery code is here:

<script>
    $(function() {

          $("#bb.").click(function () {
          $("#commentbox").toggle("slow");
          });

    });
</script>

But here only the first div toggles inside the for loop. Jquery does not work for the remaining for loop elements. Can you please give me the proper jQuery code. Thanks.


make this change:

<button class="bb" id="bb{{feed.id}}">Add Comment</button>
<div class="commentbox" style="display:none;">

and this one:

$(document).ready(function() {
  $(".bb").each(function(){
    $(this).click(function () {
      $(this).next().toggle("slow");
    });
  });
});

Update: And here is a working demo.