views:

58

answers:

4

I have a link button that I want to change it onclick callback function when clicked:

I am using .attr() to change the onclick event but it simply doesn't work.

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

But it simply doesn't work.

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

+2  A: 

Use "click()" instead of attr()

Jason
+1  A: 

Why are you attaching the event with an attr?

Why not just do like so:

$('#' + test).bind("click", function()
{
    //functionality that you were trying to add using attr can now go inside here.
});
spinon
...or just `.click(function () { /* ... */ })` instead of bind.
Matt Ball
Thanks, working great
Joseph Ghassan
@Bears yeah you are right. That is the shorthand way to write what bind does.
spinon
+3  A: 
$('#' + test).click(function() {
    return deleteLikeComment(...);
}); 
fearofawhackplanet
perfect, working now
Joseph Ghassan
A: 

If you want to switch the callback method every time the link is clicked, use .toggle(). It alternates between the two provided functions.

Felix Kling