Prevent Default Event Actions Using MooTools 1.2
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.
![Animated 3D Flipping Menu with CSS]()
CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more. I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...
![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...
![CSS Circles]()
A while back I shared a clever technique for creating triangles with only CSS. Over the past year, I've found CSS triangles incredibly effective, especially when looking to create tooltips or design elements with a likewise pointer pattern. There's another common shape...
![jQuery Chosen Plugin]()
Without a doubt, my least favorite form element is the SELECT element. The element is almost unstylable, looks different across platforms, has had inconsistent value access, and disaster that is the result of multiple=true is, well, a disaster. Needless to say, whenever a developer goes...
You can use the
$lambdafunction 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)); });@thomasd: Sweet tip!
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; })