5

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.

4 Answers 4

10

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.
});
Sign up to request clarification or add additional context in comments.

2 Comments

...or just .click(function () { /* ... */ }) instead of bind.
@Bears yeah you are right. That is the shorthand way to write what bind does.
5
$('#' + test).click(function() {
    return deleteLikeComment(...);
}); 

Comments

4

Use "click()" instead of attr()

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.