Book Image

MooTools 1.2 Beginner's Guide

Book Image

MooTools 1.2 Beginner's Guide

Overview of this book

MooTools is a simple-to-use JavaScript library, ideal for people with basic JavaScript skills who want to elevate their web applications to a superior level. If you're a newcomer to MooTools looking to build dynamic, rich, and user-interactive web site applications this beginner's guide with its easy-to-follow step-by-step instructions is all you need to rapidly get to grips with MooTools.
Table of Contents (14 chapters)
MooTools 1.2 Beginner's Guide
Credits
About the Authors
About the Reviewer
Preface

Time for action—firing off a click event


Imagine that you have a hyperlink with a click event listener attached to it, that when triggered, alerts the user with a message. But you also want to fire off this alert message when the user presses the Ctrl key. Here's how you'd do this:

  1. First, let us place a hyperlink in an HTML document. We'll put it inside a<p> element and tell the users that clicking on the hyperlink or pressing the Ctrl key will open up an alert dialog box.

    <body>
    <p>Show a warning by clicking on this link: <a href="#">Click me</a>. Alternatively, you can show the warning by pressing the <strong>Ctrl</strong> key on your keyboard.</p>
    </body>
    
  2. Next, let's add an event to<a> elements. We'll use the addEvent method to do this.

    // Add a click event
    $$('a').addEvent('click', function(){
    alert('You either clicked a link or pressed the Ctrl key.');
    });
    
  3. Now we have to add another event listener onto our HTML document that watches...