1

I have a button with a click event (from a 3. party library) which submits a form. I like to remove the click event, add my own function and call the original event after a validation.

I thought i just add an event.stopImmediatePropagation(); but that did not work. Maybe because of the order the events where added(?).

Is the another way to manage the event execution?

Or how can I get the old event to do something like this:

originalClickEvent = $('#button').doSomeMagicAndGetTheEvent('click');
$('#button').unbind();
$('#button').bind('click', function (event) {
  if (valid()) originalClickEvent();
});
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180

2 Answers2

1

Look here Remove all JavaScript event listeners of an element and its children?

After you remove the event listeners you can attach your custom event.

If I've understood you correctly this is the effect you're searching for: http://jsfiddle.net/ftGHq/

Community
  • 1
  • 1
Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
0

In case the click event is just bound to one function you could overwrite that function:

var oldFunction = theOldFunction;
function myFunction(control) {

    oldFunction(control);
}

$('#button').unbind();
$('#button').click(myFunction); 
Alex
  • 32,506
  • 16
  • 106
  • 171