Fixed a grammatical error in Element.Event (build-in to built-in
[mootools.git] / Docs / Element / Element.Event.md
blob302693cc2bfe22899aba3c0402a3530f92d17514
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](#Event:stop).
44 - This method is also attached to Document and Window.
46 ### See Also:
48 - [w3schools Event Attributes](http://www.w3schools.com/html/html_eventattributes.asp)
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 #### See Also:
305 - [Element:addEvent](#Element:addEvent)
307 ### Event: mousewheel {#Element-Events:mousewheel}
309 This event fires when the mouse wheel is rotated;
311 #### Examples:
313         $('myElement').addEvent('mousewheel', myFunction);
315 #### Notes:
317 - This custom event just redirects DOMMouseScroll (Mozilla) to mousewheel (Opera, Internet Explorer), making it work across browsers.
319 #### See Also:
321 - [Element:addEvent](#Element:addEvent)
325 Deprecated Functions {#Deprecated-Functions}
326 ============================================
329 [$]: /core/Element/Element#Window:dollar
330 [Function]: /core/Types/Function
331 [Function:bind]: /core/Types/Function/#bind
332 [Function:pass]: /core/Types/Function/#pass
333 [Function:delay]: /core/Types/Function/#delay