Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery override event

I have an anchor like this

<a href='#' onclick='return showform(this)'>click me</a>

But I want to be able to override the onclick function, how to do this in jquery?

because it seems when I add

$('a').click( function() { alert('hi there!'); } );

the new click handler is not overriding the old one

like image 622
strike_noir Avatar asked Dec 18 '09 12:12

strike_noir


People also ask

How to override click event in jQuery?

Use the off() method to override jQuery event handlers. This method is used to remove an event handler. The on() method is used to attach one or more event handler.

How to override function in jQuery?

You simply get a reference to the function "getEditor" and assign it another function. getEditor:function(){ // new function will override the existing reference that getEditor already has. } Show activity on this post. You must get object where getEditor function stored, and use reference to change it.

How to unbind event in jQuery?

jQuery unbind() MethodUse the off() method instead. The unbind() method removes event handlers from selected elements. This method can remove all or selected event handlers, or stop specified functions from running when the event occurs. This method can also unbind event handlers using an event object.


1 Answers

In your case the onCick event is overriding the jQuery one. What you might be able to do is:

$('a').unbind('click').click(function(){
    alert('why hello there children.');
})

But I believe this would have to be included after the

<a href='#' onclick='return showform(this)'>click me</a>

That said, you should really not be using onClicks anyway... it makes the code really hard to maintain and change (as you have found out).

like image 138
SeanJA Avatar answered Oct 27 '22 08:10

SeanJA