tags:

views:

60

answers:

4

I have one link button with a click event. When Clicked I want to change its click event handler to call another method. I am using this :

$('#' + test).bind('click', function () { return LikeComment(test, CommentsWrapper, activityID, textAreaID, btnAddCommentID, addCommentBox, textUnLike, textLike, likeWrapperID); });

The next time it is clicked I bind it with this :

 $('#' + test).bind('click', function () { return UnLikeComment(test, CommentsWrapper, activityID, textAreaID, btnAddCommentID, addCommentBox, textUnLike, textLike, likeWrapperID); });

The thing is the handlers are buffering. the second time I click the link, the 2 methods are called.

Nothing helped. I used unbind() same thing.

Any suggestions ?

More Info

The initial status of the link button is like this :

  <a class='activitysmalllink' href='javascript:void' id='{0}' onclick='return LikeComment( ... ) '

When I click the LIKE link button, I invoke this code :

  $('#' + test).bind('click', function () { return deleteLikeComment(test, CommentsWrapper, activityID, textAreaID, btnAddCommentID, addCommentBox, textUnLike, textLike, likeWrapperID); });

 $('#' + test).text(textUnLike);

the weird thing is that this method is invoked as soon as I bind the link using the above following code.

A: 

You need to unbind click before you re-bind it.

Just put .unbind('click') in between $('#' + test) and .bind('click' for each statement.

Scott
I just did that. It keeps buffering the callbacks.
Joseph Ghassan
A: 

i think the problm is with $('#' + test). define a variable first as var a='#' + test; then give the jquery statement as $(a)..

pahnin
sorry, same effect.
Joseph Ghassan
you can use toggle as matt said but with a currection give a click function adn inside it give the toggle function correct me if I am wrong
pahnin
<a class='activitysmalllink' href='javascript:void' id='{0}' onclick='change();'></a> <script type="javascript">change(){if($(a).attr("onclick")=="return LikeComment( ...)"){$(a).attr("onclick")=="return UnLikeComment( ... ) }else{if($(a).attr("onclick")=="return LikeComment( ... )};</script>
pahnin
phanin, thanks for the help.
Joseph Ghassan
+2  A: 

Something is messing up with your unbind: can you provide more code?

As an alternative (and prehaps, bette solution), how about toggle() instead?

$('#' + test).toggle(function () { return LikeComment(test, CommentsWrapper, activityID, textAreaID, btnAddCommentID, addCommentBox, textUnLike, textLike, likeWrapperID); },
                     function () { return UnLikeComment(test, CommentsWrapper, activityID, textAreaID, btnAddCommentID, addCommentBox, textUnLike, textLike, likeWrapperID); });

You could also use one() instead of bind()

Matt
What does toggle does ?. My scenario is that I have a like link button, when the user click it, I will change it to UnLike and change its click handler.
Joseph Ghassan
I tried one() instead of bind() same effect.
Joseph Ghassan
@Joseph: Read the documentation I linked to. Toggle will alternate between the two functions on each click. It's impossible for `one()` to behave like that, it only fires' the event once. **Post more code**.
Matt
Matt, thanks for the help man. this did the trick : $('#' + test).attr('onclick', '').unbind('click').bind('click', function () {
Joseph Ghassan
+2  A: 

Unbind() unbinds events bound before with jQuery's bind.
But in your case the event initially is listed inside the element

<a onclick='return LikeComment...

...so it would'nt be affected by unbind()

Use attr() instead of unbind()

$('#' + test).attr('onclick','').bind('click','...')

...before binding the first time. if you dont want to check if it's the first call, you could always use

$('#' + test).attr('onclick','').unbind('click').bind('click','...')
Dr.Molle
Thanks for the tip. this code solved it : $('#' + test).attr('onclick', '').unbind('click').bind('click', function () {
Joseph Ghassan
But it would also be a good solution to use the suggested toggle(). You just have to look, if there is a onclick-attribute present, if yes, remove it like shown above and bind the toggle, so you dont need any further bindings :)
Dr.Molle