add server testing in node 0.10, 0.12 and iojs
[mootools.git] / Docs / Element / Element.Event.md
blob1688daf6412f46a34b926b7d0b0845848a1c3c14
1 Type: Element {#Element}
2 ==========================
4 - Custom Type to allow all of its methods to be used with any DOM element via the dollar function [$][].
5 - These methods are also available on window and document.
7 ### Notes:
8 - Internet Explorer fires element events in random order if they are not fired by [Element:fireEvent](#Element:fireEvent).
11 Element Method: addEvent {#Element:addEvent}
12 --------------------------------------------
14 Attaches an event listener to a DOM element.
16 ### Syntax:
18         myElement.addEvent(type, fn);
20 ### Arguments:
22 1. type - (*string*) The event name to monitor ('click', 'load', etc) without the prefix 'on'.
23 2. fn   - (*function*) The function to execute.
25 ### Returns:
27 * (*element*) This Element.
29 ### Examples:
31 ##### HTML:
33         <div id="myElement">Click me.</div>
35 ##### JavaScript
37         $('myElement').addEvent('click', function(){
38                 alert('clicked!');
39         });
41 ### Notes:
43 - You can stop the Event by returning false in the listener or calling [Event:stop][].
44 - This method is also attached to Document and Window.
46 ### See Also:
48 - [MDN DOM Event Reference](https://developer.mozilla.org/en/DOM/DOM_event_reference)
52 Element Method: removeEvent {#Element:removeEvent}
53 --------------------------------------------------
55 Works as Element.addEvent, but instead removes the specified event listener.
57 ### Syntax:
59         myElement.removeEvent(type, fn);
61 ### Arguments:
63 1. type - (*string*) The event name.
64 2. fn   - (*function*) The function to remove.
66 ### Returns:
68 * (*element*) This Element.
70 ### Examples:
72 #### Standard usage:
74         var destroy = function(){ alert('Boom: ' + this.id); } // this refers to the Element.
75         $('myElement').addEvent('click', destroy);
77         //later...
78         $('myElement').removeEvent('click', destroy);
81 #### Examples with bind:
83         var destroy = function(){ alert('Boom: ' + this.id); }
84         var boundDestroy = destroy.bind($('anotherElement'));
85         $('myElement').addEvent('click', boundDestroy);
87         //later...
88         $('myElement').removeEvent('click', destroy); // this won't remove the event.
89         $('myElement').removeEvent('click', destroy.bind($('anotherElement')); // this won't remove the event either.
90         $('myElement').removeEvent('click', boundDestroy); // the correct way to remove the event.
92 ### Notes:
94 - When the function is added using [Function:bind][] or [Function:pass][], etc, a new reference is created.  For removeEvent to work, you must pass a reference to the exact function to be removed.
95 - This method is also attached to Document and Window.
99 Element Method: addEvents {#Element:addEvents}
100 ----------------------------------------------
102 The same as [Element:addEvent](#Element:addEvent), but accepts an object to add multiple events at once.
104 ### Syntax:
106         myElement.addEvents(events);
108 ### Arguments:
110 1. events - (*object*) An object with key/value representing: key the event name, and value the function that is called when the Event occurs.
112 ### Returns:
114 * (*element*) This Element.
116 ### Examples:
118         $('myElement').addEvents({
119                 mouseover: function(){
120                         alert('mouseover');
121                 },
122                 click: function(){
123                         alert('click');
124                 }
125         });
127 ### Notes:
129 - This method is also attached to Document and Window.
131 ### See Also:
133 - [Element:addEvent](#Element:addEvent)
137 Element Method: removeEvents {#Element:removeEvents}
138 ----------------------------------------------------
140 Removes all events of a certain type from an Element. If no argument is passed, removes all events of all types.
142 ### Syntax:
144         myElements.removeEvents([events]);
146 ### Arguments:
148 1. events - (optional) if not passed removes all events from the element.
149         - (*string*) The event name (e.g. 'click'). Removes all events of that type.
150         - (*object*) An object of type function pairs. Like the one passed to [Element:addEvent](#Element:addEvent).
152 ### Returns:
154 * (*element*) This Element.
156 ### Examples:
158         var myElement = $('myElement');
159         myElement.addEvents({
160                 mouseover: function(){
161                         alert('mouseover');
162                 },
163                 click: function(){
164                         alert('click');
165                 }
166         });
168         myElement.addEvent('click', function(){ alert('clicked again'); });
169         myElement.addEvent('click', function(){ alert('clicked and again :('); });
170         //addEvent will keep appending each function.
171         //Unfortunately for the visitor, there will be three alerts they'll have to click on.
172         myElement.removeEvents('click'); // saves the visitor's finger by removing every click event.
174 ### Notes:
176 - This method is also attached to Document and Window.
178 ### See Also:
180 - [Element:removeEvent](#Element:removeEvent)
182 Element Method: fireEvent {#Element:fireEvent}
183 ----------------------------------------------
185 Executes all events of the specified type present in the Element.
187 ### Syntax:
189         myElement.fireEvent(type[, args[, delay]]);
191 ### Arguments:
193 1. type  - (*string*) The event name (e.g. 'click')
194 2. args  - (*mixed*, optional) Array or single object, arguments to pass to the function. If more than one argument, must be an array.
195 3. delay - (*number*, optional) Delay (in ms) to wait to execute the event.
197 ### Returns:
199 * (*element*) This Element.
201 ### Examples:
202         // fires all the added 'click' events and passes the Element 'anElement' after one second
203         $('myElement').fireEvent('click', $('anElement'), 1000);
205 ### Notes:
207 - This will not fire the DOM Event (this concerns all inline events ie. onmousedown="..").
208 - This method is also attached to Document and Window.
210 Element Method: cloneEvents {#Element:cloneEvents}
211 --------------------------------------------------
213 Clones all events from an Element to this Element.
215 ### Syntax:
217         myElement.cloneEvents(from[, type]);
219 ### Arguments:
221 1. from - (*element*) Copy all events from this Element.
222 2. type - (*string*, optional) Copies only events of this type. If null, copies all events.
224 ### Returns:
226 * (*element*) This Element.
228 ### Examples:
230         var myElement = $('myElement');
231         var myClone = myElement.clone().cloneEvents(myElement); // clones the element and its events
233 ### Notes:
235 - This method is also attached to Document and Window.
237 Object: Element.Events {#Element-Events}
238 ========================================
240 You can add additional custom events by adding properties (objects) to the Element.Events Object
242 ### Arguments:
244 The Element.Events.yourProperty (object) can have:
246 1. base - (*string*, optional) the base event the custom event will listen to. Its not optional if condition is set.
247 2. condition - (*function*, optional) the condition from which we determine if the custom event can be fired. Is bound to the element you add the event to. The Event is passed in.
248 3. onAdd - (*function*, optional) the function that will get fired when the custom event is added. Is bound to the element you add the event to.
249 4. onRemove - (*function*, optional) the function that will get fired when the custom event is removed. Is bound to the element you add the event to.
251 ### Examples:
253         Element.Events.shiftclick = {
254                 base: 'click', // the base event type
255                 condition: function(event){ //a function to perform additional checks
256                         return (event.shift == true); // this means the event is free to fire
257                 }
258         };
260         $('myInput').addEvent('shiftclick', function(event){
261                 log('the user clicked the left mouse button while holding the shift key');
262         });
264 ### Notes:
266 - There are different types of custom Events you can create:
267  1. Custom Events with only base: they will just be a redirect to the base event.
268  2. Custom Events with base and condition: they will be redirect to the base event, but only fired if the condition is met.
269  3. Custom Events with onAdd and/or onRemove and any other of the above: they will also perform additional functions when the event is added/removed.
270 - Since MooTools 1.3 this is a native JavaScript Object and not an instance of the deprecated Hash
272 ### Warning:
274 If you use the condition option you NEED to specify a base type, unless you plan to overwrite a native event.
275 (highly unrecommended: use only when you know exactly what you're doing).
279 Built-in Custom Events
280 ----------------------
283 ### Event: mouseenter {#Element-Events:mouseenter}
285 This event fires when the mouse enters the area of the DOM Element and will not be fired again if the mouse crosses over children of the Element (unlike mouseover).
287 #### Examples:
289         $('myElement').addEvent('mouseenter', myFunction);
291 #### See Also:
293 - [Element:addEvent](#Element:addEvent)
295 ### Event: mouseleave {#Element-Events:mouseleave}
297 This event fires when the mouse leaves the area of the DOM Element and will not be fired if the mouse crosses over children of the Element (unlike mouseout).
299 #### Examples:
301         $('myElement').addEvent('mouseleave', myFunction);
303 #### Notes:
305 - `mouseenter` and `mouseleave` events are supported natively by Internet Explorer, Opera 11, and Firefox 10. MooTools will only add the custom events if necessary.
307 #### See Also:
309 - [Element:addEvent](#Element:addEvent)
311 ### Event: mousewheel {#Element-Events:mousewheel}
313 This event fires when the mouse wheel is rotated;
315 #### Examples:
317         $('myElement').addEvent('mousewheel', myFunction);
319 #### Notes:
321 - This custom event just redirects DOMMouseScroll (Mozilla) to mousewheel (Opera, Internet Explorer), making it work across browsers.
323 #### See Also:
325 - [Element:addEvent](#Element:addEvent)
328 Object: Element.NativeEvents {#Element-NativeEvents}
329 ====================================================
331 This is an object with all known DOM event types, like click, mouseover, load, etc.
332 Each event type has a value, possible values are `0` (`undefined`, `null`), `1`, and `2`.
334 ### Type 0 Events
336 By default it is undefined. In this case you can add events, but you should manually fire them.
338 #### Example:
340         element.addEvent('pizza', fn);
341         element.fireEvent('pizza', 'yum!');
343 The event is not actually added to the DOM, but is only registered in a JS object.
345 ### Type 1 Events
347 The second case is if the value is 1. This time the object is attached to the DOM. Usually by element.addEventListener, or element.attachEvent in older versions of IE. You can still use `element.fireEvent('load')` to manually fire events.
349 ### Type 2 Events
351 The final case is if the value is 2. This is the same as case 1. The only difference is that the event object, containing interesting data, is wrapped and normalized by event wrapper ([DOMEvent][]). This is the most used variant, for mouse events (like *click*) and keyboard events.
353 The reason to differentiate between 1 and 2 is that 1 is usually used for events that don't have interesting data like: `onload`, `onscroll`, and `onresize`, or it's more performant. The latter two, for example, are fired frequently.
355 ### Adding unsupported events
357 Not all events are supported by MooTools' Element Events API because of edge use cases or new events supported by the browser. To add support for a native event, just augment the `Element.NativeEvents` object with the key and **appropriate** key value (use the above). For example to add `popstate` support in your application:
359         Element.NativeEvents.popstate = 2;
360         // Now element.addEvent('popstate', fn); will work everywhere
363 [$]: /core/Element/Element#Window:dollar
364 [Event:stop]: /core/Types/Event#Event:stop
365 [Function]: /core/Types/Function
366 [Function:bind]: /core/Types/Function/#bind
367 [Function:pass]: /core/Types/Function/#pass
368 [Function:delay]: /core/Types/Function/#delay
369 [DOMEvent]: /core/Types/DOMEvent