Treehouse

MooTools Tip: Event.stop

By on  

Events are a huge part of JavaScript because so much of our JavaScript is triggered by the user's actions. Quite frequently we will assign a listener to a link and immediately want the default click action stopped. For example:

$('myLink').addEvent('click',function(e) {
	e.stop();
	//do stuff
});

The above code works great but there's one small hole I sometimes run into. On rare occasion I feel the need to trigger an event on an element:

$('myLink').fireEvent('click');

The problem is that the Event object is not given when an event is triggered using Element.fireEvent. The way to prevent any problems is an easy if check:

$('myLink').addEvent('click',function(e) {
	if(e) e.stop();
	//do stuff
});

Voila. The safe way to stop events!

ydkjs-2.png

Recent Features

  • 9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

  • Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • Duplicate the jQuery Homepage Tooltips Using Dojo

    The jQuery homepage has a pretty suave tooltip-like effect as seen below: Here's how to accomplish this same effect using Dojo. The XHTML The above HTML was taken directly from the jQuery homepage -- no changes. The CSS The above CSS has been slightly modified to match the CSS rules already...

  • jQuery Comment Preview

    I released a MooTools comment preview script yesterday and got numerous requests for a jQuery version. Ask and you shall receive! I'll use the exact same CSS and HTML as yesterday. The XHTML The CSS The jQuery JavaScript On the keypress and blur events, we validate and...

Discussion

  1. Alex

    Nice one, thanks.
    I was using e.preventDefault()
    :)

  2. Doesn’t “return false;” handle this just as well (assuming you’re only adding one function to the event)?

  3. I’m using new Event(e).stop();

  4. nice trick,
    I was facing this problem a lot of them,
    Thanks.

  5. @David Walsh: Thanks, that’s good info. I hadn’t run into that as an issue, but will keep this in mind going forward.

  6. I do this all the time, it’s always nice to see other developers handling it the same way.

  7. I prefer the syntax, considering it saves a whole byte:
    e && e.stop();

    :)

  8. Great solution. I spent a day trying to find a solution on similar problem. David you can’t imagine how much I love your blog.

  9. Granted you may have some special reason for fake-firing an event in which case your solution is fine. But more generally a better practise is to have your event handler call a function. So if you need to make that function happen, just call your function from code rather than faking the event and tripping the event handler in a fake way. Maybe you can give an example of when it’s not possible to use my suggestion?
    Thanks,
    Paul S

  10. @Paul Schwarz: Your solution require to remember what function is connected to event. Sometimes it’s anonymous functions and you can’t call it directly. Little example:

    //add action to all links on page
    $$('a').addEvent('click', function (e) {alert('My href is'+this.get('href'));});
    
    $$('a')[0].fireEvent('click');
    
  11. @Paul Schwarz: Another example is, you can have plugins on page that do something with your elements, and don’t have access to this functions. It can be more that one function connected to element, and from other sources than yours. So basically calling: myelement.fireEvent you will call all functions attached to the event, and you don’t have to remember or search for it.

  12. Paris

    e.stop(); works for me as well. What I am looking for is a way to kill all the events (never to fire again). Would help a lot to deal with app “crashes” when timers and mouse, etc. events are involved.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!