Queen13
Queen13

Reputation: 25

Jquery .Click event only triggering first button in the loop

Jquery .click event only triggers the first button! I loop through data and add buttons to each on my html page and would like each button to trigger a dialog box when clicked.. But only the first one works! The rest seem to have no click event.

$(document).ready(function () {

$("#btn_comment").click(function () {

    $("#createComment").dialog(
                            {
                                modal: true,
                                height: 300,
                                width: 500,
                                buttons: {
                                    "Create a Comment": function () {
                                    var post_id = $(this).parent().attr("id");
                                    var desc_to_create = $("#txtComment").val();
                                    $.post("CreateComment", { "id": "", "username": "x", "post_id": post_id, "description": desc_to_create, "created": "" }, function (t) {

                                            alert("Thank you! Your comment has been updated!!");
                                            location.reload();

                                        })


                                    },
                                    "Cancel": function () {
                                        $(this).dialog("close");
                                    }
                                }
                            }
                            );
})

})
    <tr id='<%= Html.Encode(item.id) %>'>
        <td>

            <%: Html.ActionLink("Details", "Details", New With {.id = item.id})%> |
              <a href="javascript://" class="delete_btn">Delete</a>

        </td>
      <%--  <td>
            <%: item.id %>
        </td>
        <td>
            <%: item.username %>
        </td>
        <td>
            <%: item.title %>
        </td>--%>
        <td>
            <%: item.description %>
        </td>
        <td>
            <input id="btn_comment" type="button" value="Add a Comment" />
        </td>
        <td>
            <div id="new_comment"></div></td>
    </tr>

<% Next%>

Upvotes: 0

Views: 1488

Answers (1)

David
David

Reputation: 219077

Your HTML markup is invalid. You have this in a loop:

<input id="btn_comment" type="button" value="Add a Comment" />

However, HTML requires that id values be unique. I imagine the behavior you're seeing (jQuery finding only the first matching element) is browser-specific, because it's undefined.

If your elements aren't unique, the shortest path to solve this is probably to use a class instead of an id. Something like this:

<input class="btn_comment" type="button" value="Add a Comment" />

Then your jQuery selector would be:

$('.btn_comment')

This would select every matching element.

Naturally, this assumes that you're not using that id for anything else. (And, if you are, you're going to want to re-work that logic anyway because the markup is invalid.)

Upvotes: 2

Related Questions