jeffchannell.com

Stop Propagating Events in Mootools 1.11

Posted in JavaScript
2009-07-20 15:37:07 +0000 UTC

If you've ever done any scripting using the Mootools library that ships with Joomla, perhaps you've come across a situation where you needed to prevent a link from following it's assigned location. Mootools has a function for Events called 'stop', but it is not extended to Events in 1.11. So how do you stop the link from following it's href?

The answer is to create a new Event using the already existing Event, and use the 'stop' method on that. Here's a simple script that shows this in action:

window.addEvent( 'load', function( e )
{
  $$('#targetdiv a').each( function( el )
  {
    el.addEvent( 'click', function( ce )
    {
      new Event( ce ).stop();
      alert( el.getProperty( 'href' ) );
    });
  });
});

Here's an example to show it in action: