Adding onclick attribute to <a> tag using jQuery

I have a link button in which I want to change its onclickcallback function when pressed:

I use .attr()to change events onclick, but it just doesn't work.

$('#' + test).text('Unlike');
$('#' + test).attr("onclick", "return deleteLikeComment('"
        + CommentsWrapper + "','" + activityID + "', '" + test + "');");

But that just doesn't work.

I just want to switch the callback method when the link is clicked.

+5
source share
4 answers

Why are you attaching an event with attr?

Why not just do this:

$('#' + test).bind("click", function()
{
    //functionality that you were trying to add using attr can now go inside here.
});
+10
source
$('#' + test).click(function() {
    return deleteLikeComment(...);
}); 
+5
source

click()" attr()

+4
+2

All Articles