1

Suppose I need to write an onclick handler in an attribute, like this:

<a href='#' onclick='DoSomething(); event.stopPropagation(); return false;'>
    ...</a>

I know this is bad form, but suppose for the moment that I have to use an attribute.

Unfortunately event is not defined. I tried e and that is not defined either. Is there any way to access the event object inside the attribute, or any other way to stop the event propagation?

2
  • 1
    Works fine for me. Which browser are you using? Commented Aug 7, 2011 at 21:08
  • This JSFiddle only shows one alert box on Google Chrome (Mac, 13.0.782.107): jsfiddle.net/xfZvK Commented Aug 7, 2011 at 21:11

3 Answers 3

3

Where you get the event object depends on the browser. Some (all?) IE versions put it in window.event but everyone else passes it as an argument. So, you end up having to do a silly little dance like this:

function DoSomething(event) {
    event = event || window.event;
    event.stopPropagation();
    // Do useful work.
    return false;
}

And then set up the onclick like this:

<a href="javascript:void(0)" onclick="DoSomething(event)">

I also switch to javascript:void(0) so you don't have the usual "# interpreted as an in-page fragment" problems.

Sign up to request clarification or add additional context in comments.

2 Comments

Please use var event = ....
@pimvdb: Why? Having it in the function's argument list is sufficient to avoid an accidental global.
1

I believe you need to pass it into the function itself, like so:

<a href='#' onclick='DoSomething(event); return false;'>...</a>

function DoSomething(event){
    event.stopPropagation(); 
}

7 Comments

This makes no sense. Where did you get the event reference from in the first place? And if you do have it, why can't you use it in the onclick attribute directly?
Whoa, this actually worked. Sorry about the earlier downvote, I did this wrong...
@Samir: I wish I could downvote a comment, you're not making any sense. Try this in a non-IE browser: jsfiddle.net/ambiguous/KSnmP
@Timwi I edited the answer. Feel free to remove your downvote. Thanks.
@mu: That works fine. As does <a href="#" onclick="console.log(event);">pancakes</a>. There's no need for the function. If you have a reference to event, you can use it anywhere. Not just in a function.
|
-1

Depending on browser, event creating and handling is handled differently.

IE creates the global event object when the click happens. FF doesn't create the global event object but passes it to the function as the first argument.

But in your case, simply saying return false will cancel the link action (if that's what you intend to do).

<a href='#' onclick='DoSomething(); return false;'>    ...</a>

4 Comments

@Timwi: What browser are you using? It is not undefined in IE9
The window.event thing is an old IE-ism. Not my downvote though.
@Mrchief: If IE is the only browser in which you test your answers before you post them, you need to rethink. Also, your edited post is still wrong, return false does not cancel the bubbling, it only cancels the link’s click action (to follow the link).
@Timwi: You're right. I meant cancelling the link action, I'll edit my post to correct this. I also (wrongly) presumed that you knew about the event argument, just that for some reason you wanted to do it in the attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.