Add multiple Events to an Element, create custom Events and fire an Event : Action « Mootools « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Dojo toolkit
7. Event
8. Event onMethod
9. Ext JS
10. Form Control
11. GUI Components
12. HTML
13. Javascript Collections
14. Javascript Objects
15. Javascript Properties
16. jQuery
17. Language Basics
18. Mochkit
19. Mootools
20. Node Operation
21. Object Oriented
22. Page Components
23. Rico
24. Scriptaculous
25. Security
26. SmartClient
27. Style Layout
28. Table
29. Utilities
30. Window Browser
31. YUI Library
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript DHTML » Mootools » Action 
Add multiple Events to an Element, create custom Events and fire an Event
 

<!--
MooTools is released under the Open Source MIT license, 
which gives you the possibility to use it and modify 
it in every circumstance. 
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style rel="stylesheet" type="text/css">
textarea {
  float: left;
  background: #f8f8f8;
  border: 1px solid #d6d6d6;
  border-left-color: #e4e4e4;
  border-top-color: #e4e4e4;
  padding: 0.3em;
  margin-top: 10px;
  overflow: auto;
}
 
#log {
  float: left;
  padding: 0.5em 0em 0.2em;
  margin-left: 10px;
  width: 290px;
  height: 50px;
  border: 1px solid #d6d6d6;
  border-left-color: #e4e4e4;
  border-top-color: #e4e4e4;
  margin-top: 10px;
  visibility: hidden;
  font-size: 25px;
  font-weight: bold;
  text-align: center;
}  
  
  </style>
  <script type="text/javascript" src="mootools.js"></script>
  <script type="text/javascript">
window.addEvent('domready', function() {
  var textarea = $('myTextarea'), log = $('log');
  
  // We define the highlight morph we're going to
  // use when firing an event
  var highlight = new Fx.Morph(log, {
    duration: 1500,
    link: 'cancel',
    transition: 'quad:out'
  });
   
  // Here we start adding events to textarea.
  // Note that 'focus' and 'keyup' are native events, while 'burn'
  // is a custom one we've made
  textarea.addEvents({
    focus: function() {
      // When focusing, if the textarea contains value "Type here", we
      // simply clear it.
      if (textarea.value.contains('Type here')) textarea.value = '';
    },
    
    keyup: function() {
      // When user keyups we check if there are any of the magic words.
      // If yes, we fire our custom event burn with a different text for each one.
      if   (textarea.value.contains('hello')) textarea.fireEvent('burn', 'hello world!');
      else if (textarea.value.contains('moo')) textarea.fireEvent('burn', 'mootools!');
      else if (textarea.value.contains('pizza')) textarea.fireEvent('burn', 'Italy!');
      else if (textarea.value.contains('burn')) textarea.fireEvent('burn', 'fireEvent');
      // note that in case of 'delayed', we are firing the event 1 second late.
      else if (textarea.value.contains('delayed')) textarea.fireEvent('burn', "I'm a bit late!"1000);
    },
    
    burn: function(text) {
      // When the textarea contains one of the magic words
      // we reset textarea value and set the log with text
      textarea.value = ''; log.set('html', text);
      
      // then we start the highlight morphing
      highlight.start({
        backgroundColor: ['#fff36f', '#fff'],
        opacity: [10]
      });
    }
  });
});  
  </script>
  <title>Elements Event Demo</title>
</head>
<body>
  <h1>Events</h1>
  <h2>Introduction</h2>
  <p>
    This demo will show you how to: add multiple Events to an Element, create custom Events and fire an Event.
  </p>
  <p>
    In below textarea you can type whatever. Typing one of listed "Magic Words" will cause Event "burn" to fire and starting an effect (see js code)
  </p>
  <p>
    Magic Words to type: <strong>hello</strong>, <strong>moo</strong>, <strong>pizza</strong>, <strong>burn</strong>, <strong>delayed</strong> (this last one will fire after second).
  </p>
  <div id="inner_demo">
    <textarea id="myTextarea" name="textarea" cols="40" rows="6">Type here...</textarea>
  </div>
  <div id="log">
    <!-- spacer -->
  </div>
</body>
</html>


   
     
  
Related examples in the same category
1. With MooTools it is easy to define often used actions as custom events for elements
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.