Clicking in the yellow box will give hello world feedback.
Clicking on the first link will take you to the YUI website; clicking on the
second link, which has the same href
attribute, will display
a message instead, and not navigate to a new page.
Event Utility is used here to do two things:
click
event handler to the yellow box;<a>
element that uses preventDefault()
to prevent the link,
when clicked, from navigating to a new page. To illustrate event handling syntax, we'll create a `
Hello World!
"); Y.one('#container').addClass('hello'); } ... ```We now use the Node's on
method to attach our
helloWorld
function as a handler for the click
event.
```
// Point to the container div with the CSS selector
var node = Y.one('#container');
node.on("click", helloWorld);
```
Almost all event handling begins with a premise just this simple: we have an
element to which something might happen (eg, it might be clicked) and, when
that does happen, we want to do something (eg,
helloWorld
).
In some cases, you may want to replace the default behavior of an event. For example, let's say you have two links on the page:
```The YUI Library. (Link navigates away from page.)
The YUI Library. (Link's default behavior is suppressed.)
```Let's say that when the second link is clicked, instead of navigating away
from the current page, you want to display a message.
The event object passed to your event handler is a facade — not
the actual browser event object.
This facade supports the preventDefault
method for cancelling
inherent browser behavior such as anchor links loading a new page.