views:

161

answers:

4

I have added an onclick event, which opens a new tab/page, to a button which has an action already (I don't know which action cause it isn't displayed in source, or maybe it's a form submit action).

Button originally also opens a page in new tab, what I want to do is fire up some function after my onclick attribute executes which will stop execution and that default page opening will not happen, only my page will load.

Is there something that can be done?

A: 

It sounds like the event is bubbling up after you have handled it. To stop this happening, add event.cancelBubble = true at the end of your handler.

Mark Byers
How, when I only add attribute onclick, how to put cancelBubble after page rendering?
dfilkovi
A: 

Using jQuery, you can do this:

$('button').click(function() {

  //do something

  return false; // stop the event from propagating
});
nicholaides
This will not work cause button has two events, I need something that will disable other event to fire.
dfilkovi
you might want to look at event binding in jquery. You should be able to remove the event - although you do need to know what it is otherwise how can you stop it?
matpol
A: 

Try getting your function that your call on the onclick event to return false. This will cause any existing action by the button to be overridden

Sheff
But if the action is on a form it doesn't get overridden.
dfilkovi
A: 

If the handler was set using 'onclick=' you can simply rewrite the onclick property.

If a handler was added to an element with addEventHandler or attachEvent, you can remove it with removeEventHandler or detachEvent, using the same parameters that were used to set it.

If you don't know the setter, or if it used an anonymous function, you can't remove it.

You could replace the element with a duplicate that has no events and set your own on the new element.

(cloneNode is not supposed to clone events, but it sometimes does in some browsers.)

kennebec