Monday, May 14, 2012

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%>




No comments:

Post a Comment