Prevent Default Event Actions Using MooTools 1.2

By  on  

Many sweet JavaScript events are trigger by clicking on a link or a submit button. What if you don't want the browser to follow the link? What if you don't want the form to be submitted traditionally? MooTools allows you to prevent the default actions of most elements by using the preventDefault() method.

The Sample XHTML

<p><a href="http://scriptandstyle.com" class="prevent">ScriptAndStyle.com</a></p>
<p><input type="checkbox" class="prevent" /></p>
<p><input type="submit" class="prevent" value="Submit Form" /></p>

The action of any element with the prevent class will be nullified upon click.

The MooTools 1.2 Code

window.addEvent('domready', function() {
	$each($$('.prevent'),function(el) {
		el.addEvent('click',function(event) {
			event.preventDefault();
		});
	});
});

You can also prevent the browser from allowing image dragging! I did, however, notice that this function did not work correctly on a radio input.

Recent Features

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

Incredible Demos

  • By
    Add Styles to Console Statements

    I was recently checking out Google Plus because they implement some awesome effects.  I opened the console and same the following message: WARNING! Using this console may allow attackers to impersonate you and steal your information using an attack called Self-XSS. Do not enter or paste code that you...

  • By
    Fixing sIFR Printing with CSS and MooTools

    While I'm not a huge sIFR advocate I can understand its allure. A customer recently asked us to implement sIFR on their website but I ran into a problem: the sIFR headings wouldn't print because they were Flash objects. Here's how to fix...

Discussion

  1. thomasd

    You can use the $lambda function and a little bit of the Elements-Class magic to make this a one-liner:

    window.addEvent('domready', function() {
           //Now every click on one of the .prevent-Elements returns a 'false' and the propagation of the event gets stopped.
        $$('.prevent').addEvent('click',$lambda(false));
    });
    
  2. @thomasd: Sweet tip!

  3. asdf123

    i found that the method that fails the least is:

    $("bla").addEvent('click',function(_e){
    
    new Event(_e).preventDefault().stopPropagation();
    
    
    //alternatively:
    
    new Event(_e).stop();
    
    //or very simple in mootools 1.2:
    
    return false;
    
    })
    

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