To say "Hello World" using a technique that causes elements to stand at our beck and call, we must be sure our HTML DOM is complete and syntactically valid, and then we add an element that will be our trigger:
<input type="button" id="mybutton" value="Greet Me!"/>
Place a listener on that trigger element using Element.addEvent()
and pass it an anonymous function to bind to the event:
var mygreeting = 'Hello World!'; var onEvent = 'click'; // do not include the "on" in onClick $('mybutton').addEvent(onEvent,function(myevent) { // extend the event with MooTools, we may stop the event var e = new Event(myevent); e.stop(); // let us say hello in a few different ways $('mycanvas').set('text',mygreeting); // 1 write to DOM alert(mygreeting); // 2 pop up modal window.status = mygreeting; // 3 window status console.log(mygreeting); // 4 write to console });
Three of the four actions called are visible in this image...