Here is my ajax function that has var template variable.
$.ajax({
type: "GET",
url: "/Home/GetComment",
dataType: "JSON",
success:
function (comments) {
for (var i = 0; i < comments.length; i++)
{
var template = '<a id="like_button" class="like-comment" href="#">Like <span class="glyphicon glyphicon-thumbs-up"></span></a>'
}
}
});
$("#like_button").click(function (evt) {
$.ajax({
type: "POST",
url: "/Home/AddLikes",
data: { "likeid": id },
dataType: "JSON",
success:
function () {
alert('alert');
}
});
});
This click function is does not work with this template, but if I take the same template and put it in my Index view, then the click function works.
This is the code in Index view where it has the same id as the one in the template:
<a id="like_button" class="like-comment" href="#">Like <span class="glyphicon glyphicon-thumbs-up"></span></a>