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:
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>
Next, let's add an event to
<a>
elements. We'll use theaddEvent
method to do this.// Add a click event $$('a').addEvent('click', function(){ alert('You either clicked a link or pressed the Ctrl key.'); });
Now we have to add another event listener onto our HTML document that watches...