Jquery change onclick href event

I have an href element and there is an onclick event on it. I want to change a function after some event. I tried using jquery, but the old and new function are both fired. I want only the new one to be launched.

My code is:

<a href='#' id='cvtest' onclick='testMe("One")' >TEST</a>
 after some event i am adding following code:
$("#cvtest").click(function(){ testMe("Two"); });  

When I click the Test link, I get 2 warnings, One and Two.

How to stop the start of the first event or some other solution to this problem?

+5
source share
1 answer

Do not use an obsolete property onclick. Assign both event handlers using jQuery. Then it's easy to remove the one you no longer want.

// Add the original event handler:
var originalEventHandler = function() { 
    testMe('One');
};
$("#cvtest").click(originalEventHandler);

// Then later remove it and add a new one:
var newEventHandler = function() { 
    testMe('Two');
};
$("#cvtest").unbind('click', originalEventHandler).click(newEventHandler);
+6
source

All Articles