Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding onclick Attribute to <a> tag using JQuery

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.

like image 998
Joseph Ghassan Avatar asked Aug 02 '10 16:08

Joseph Ghassan


4 Answers

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.
});
like image 141
spinon Avatar answered Oct 19 '22 09:10

spinon


$('#' + test).click(function() {
    return deleteLikeComment(...);
}); 
like image 31
fearofawhackplanet Avatar answered Oct 19 '22 09:10

fearofawhackplanet


Use "click()" instead of attr()

like image 45
Jason Avatar answered Oct 19 '22 08:10

Jason


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

like image 38
Felix Kling Avatar answered Oct 19 '22 07:10

Felix Kling