0

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%>
2
  • 1
    Do you have more than one button on your page with the ID of btn_comment? IDs must be unique. Commented May 14, 2012 at 0:42
  • @j08691 You're right. He should be using a class if there's more than one. Commented May 14, 2012 at 0:44

1 Answer 1

2

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.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.