-1

I am implementing a like functionality and here is the challenge I am having.

Once the user clicks on like, the js below runs and renders a string from the server, which is a partial view. Then I have unlike link/icon there to enable user to unlike it. But I cant click on it, ie; the link is dead, doesnt fire. DEAD.

$('.like-button').click(function() {
    var btn = $(this);
    var projectid = btn.attr('id').replace('like_', '');

    $.post('/Projects/Like', {
        projectid: projectid
    }).done(function(data) {

        if (data.likePart) {
            $('#likepart_' + projectid).html(data.likePart);
        }
    });

    return false;
});

EDIT:

This is the html:

{"likePart":"\u003cspan class=\"badge bg-aqua\" id=\"count_10\"\u003e\r\n 0\r\n\u003c/span\u003e\r \n\r\n \u003ca href=\"#\" id=\"like_10\" class=\"like-button\"\u003e\r\n \u003ci class=\"fa fa-heart-o\" style=\"color: #ff0000;\"\u003e\u003c/i\u003e\r\n \u003c/a\u003e\r\n\r\n\r\n\r\n\r\n \u003cspan\u003e İlk beğenen siz olun.\u003c/span\u003e\r\n"}

How can I ensure that once the link returns, user can click on it?

Thanks.

DarthVader
  • 52,984
  • 76
  • 209
  • 300

1 Answers1

1

You should use on:

$(document/parentSelector).on('click', '.like-button', function() {
    // Your code here
});
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • Don't guess, Let OP post his HTML – Satpal May 26 '15 at 12:29
  • I was thinking same, but this will not work if the returned HTML does not contain the like-button class – AmmarCSE May 26 '15 at 12:29
  • `Then I have unlike link/icon there to enable user to unlike it. But I cant click on it, ie; the link is dead, doesnt fire. DEAD.` – Tushar May 26 '15 at 12:30
  • Its still a good guess might be correct. Just hold till OP add HTML – Satpal May 26 '15 at 12:30
  • @DarthVader learn [**Event Delegation**](http://learn.jquery.com/events/event-delegation/) `$(document).on('click', '.like-button', function () { ` – Satpal May 26 '15 at 12:35