- cherry-picked from calyptus branch:
[mootools/dkf.git] / Docs / Element / Element.md
blobf06213a351a9bc7163b3fd44941f29533c2313cc
1 Native: Window {#Window}
2 ========================
4 The following functions are treated as Window methods.
7 Function: $ {#dollar}
8 ---------------------
10 The dollar function has a dual purpose: Getting the element by its id, and making an element in Internet Explorer "grab" all the [Element][] methods.
12 ### Syntax:
14         var myElement = $(el);
16 ### Arguments:
18 1. el - The Element to be extended. Can be one of the following types:
19         * (*element*) The element will be extended if it is not already.
20         * (*string*) A string containing the id of the DOM element desired.
21         * (*object*) If the object has a toElement method, toElement will be called to get the Element.
23 ### Returns:
25 * (*element*) A DOM element.
26 * (*null*) Null if no matching id was found or if toElement did not return an element.
28 ### Examples:
30 #### Get a DOM Element by ID:
32         var myElement = $('myElement');
34 #### Get a DOM Element by reference:
36         var div = document.getElementById('myElement');
37         div = $(div); //The element with all the Element methods applied.
39 ### Notes:
41 - This method is useful when it's unclear if working with an actual element or an id.  It also serves as a shorthand for document.getElementById().
42 - In Internet Explorer, the [Element][] is extended the first time $ is called on it, and all the [Element][] Methods become available.
43 - Browsers with native HTMLElement support, such as Safari, Firefox, and Opera, apply all the [Element][] Methods to every DOM element automatically.
44 - Because MooTools detects if an element needs to be extended or not, this function may be called on the same Element many times with no ill effects.
48 Function: $$ {#dollars}
49 -----------------------
51 Selects and extends DOM elements. Elements arrays returned with $$ will also accept all the [Element][] methods.
53 ### Syntax:
55         var myElements = $$(aTag[, anElement[, Elements[, ...]);
57 ### Arguments:
59 * Any number of the following as arguments are accepted:
60  * HTMLCollections,
61  * arrays of elements,
62  * elements, or
63  * strings as selectors.
65 ### Returns:
67 * (*array*) - An array of all the DOM elements matched, extended with [$][].
69 ### Examples:
71 #### Get Elements by Their Tag Names:
73         $$('a'); //Returns all anchor elements in the page.
74         $$('a', 'b'); //Returns all anchor and bold tags on the page.
76 #### Using CSS Selectors When [Selectors][] is Included:
78         $$('#myElement'); //Returns an array containing only the element with the id 'myElement'.
79         $$('#myElement a.myClass'); //Returns an array of all anchor tags with the class 'myClass' within the DOM element with id 'myElement'.
81 #### More Complex $$ Usage:
83         //Creates an array of all elements and selectors passed as arguments.
84         $$(myelement1, myelement2, 'a', '#myid, #myid2, #myid3', document.getElementsByTagName('div'));
86 ### Notes:
88 - When [Selectors][] is loaded, [$$][] will also accept CSS Selectors.  Otherwise, the only selectors supported are tag names.
89 - If an expression doesn't find any elements, an empty array will be returned.
90 - The return type of element methods run through [$$][] is always an array, regardless of the amount of results.
92 ### See Also:
94 - See [Selectors][] for documentation on selectors for use anywhere they are accepted throughout the framework.
98 Native: Element {#Element}
99 ==========================
101 Custom Native to allow all of its methods to be used with any extended DOM Element.
105 Element Method: constructor {#Element:constructor}
106 --------------------------------------------------
108 Creates a new Element of the type passed in.
110 ### Syntax:
112         var myEl = new Element(element[, properties]);
114 ### Arguments:
116 1. element - (*mixed*) The tag name for the Element to be created or an actual DOM element.
117 2. properties - (*object*, optional) Calls the Single Argument version of [Element:set][] with the properties object passed in.
119 ### Returns:
121 * (*element*) A new MooTools extended HTML Element.
123 ### Examples:
125         var myAnchor = new Element('a', {
126                 'href': 'http://mootools.net',
127                 'class': 'myClass',
128                 'html': 'Click me!',
129                 'styles': {
130                         'display': 'block',
131                         'border': '1px solid black'
132                 },
133                 'events': {
134                         'click': function(){
135                                 alert('clicked');
136                         },
137                         'mouseover': function(){
138                                 alert('mouseovered');
139                         }
140                 }
141         });
143 ### See Also:
145 - [$][], [Element:set][]
149 Element Method: getElement {#Element:getElement}
150 ------------------------------------------------
152 Gets the first descendant element whose tag name matches the tag provided.  If [Selectors][] is included, CSS selectors may also be passed.
154 ### Syntax:
156         var myElement = myElement.getElement(tag);
158 ### Arguments:
160 1. tag - (*string*) Tag name of the element to find.
162 ### Returns:
164 * (*mixed*) If a match is found, the Element will be returned. Otherwise, returns null.
166 ### Examples:
168         var firstDiv = $(document.body).getElement('div');
170 ### Notes:
172 - This method is also available for Document instances.
173 - This method gets replaced when [Selectors][] is included.
174 - [Selectors][] enhances [Element:getElement][] so that it matches based on CSS selectors.
176 ### See Also:
178 - See [Selectors][] for documentation on selectors for use anywhere they are accepted throughout the framework.
182 Element Method: getElements {#Element:getElements}
183 --------------------------------------------------
185 Collects all decedent elements whose tag name matches the tag provided.  If [Selectors][] is included, CSS selectors may also be passed.
187 ### Syntax:
189         var myElements = myElement.getElements(tag);
191 ### Arguments:
193 1. tag - (*string*) String of the tag to match.
195 ### Returns:
197 * (*array*) An [Elements][] array of all matched Elements.
199 ### Examples:
201         var allAnchors = $(document.body).getElements('a');
203 ### Notes:
205 - This method is also available for Document instances.
206 - This method gets replaced when [Selectors][] is included.
207 - [Selectors][] enhances [Element:getElements][] so that it matches based on CSS selectors.
209 ### See Also:
211 - See [Selectors][] for documentation on selectors for use anywhere they are accepted throughout the framework.
215 Element Method: getElementById {#Element:getElementById}
216 --------------------------------------------------------
218 Gets the element with the specified id found inside the current Element.
220 ### Syntax:
222         var myElement = anElement.getElementById(id);
224 ### Arguments:
226 1. id - (*string*) The ID of the Element to find.
228 ### Returns:
230 * (*mixed*) If a match is found, returns that Element. Otherwise, returns null.
232 ### Examples:
234         var myChild = $('myParent').getElementById('myChild');
236 ### Notes:
238 - This method is not provided for Document instances as document.getElementById is provided natively.
242 Element Method: set {#Element:set}
243 ----------------------------
245 This is a "dynamic arguments" method. Properties passed in can be any of the 'set' properties in the [Element.Properties][] Hash.
247 ### Syntax:
249         myElement.set(arguments);
251 ### Arguments:
253 - Two Arguments (property, value)
254         1. property - (*string*) The string key from the [Element.Properties][] Hash representing the property to set.
255         2. value - (*mixed*) The value to set for the specified property.
256 - One Argument (properties)
257         1. properties - (*object*) Object with its keys/value pairs representing the properties and values to set for the Element (as described below).
259 ### Returns:
261 * (*element*) This Element.
263 ### Examples:
265 #### With Property and Value:
267         $('myElement').set('text', 'text goes here');
268         $('myElement').set('class', 'active');
269         //The 'styles' property passes the object to Element:setStyles.
270         var body = $(document.body).set('styles', {
271                 'font': '12px Arial',
272                 'color': 'blue'
273         });
275 #### With an Object:
277         var myElement = $('myElement').set({
278                 //The 'styles' property passes the object to Element:setStyles.
279                 'styles': {
280                         'font': '12px Arial',
281                         'color': 'blue',
282                         'border': '1px solid #f00'
283                 },
284                 //The 'events' property passes the object to Element:addEvents.
285                 'events': {
286                         'click': function(){ alert('click'); },
287                         'mouseover': function(){ this.addClass('over') }
288                 },
289                 //Any other property uses Element:setProperty.
290                 'id': 'documentBody'
291         });
293 ### Notes:
295 - All the property arguments are passed to the corresponding method of the [Element.Properties][] Hash.
296 - If no matching property is found in [Element.Properties][], it falls back to [Element:setProperty][].
297 - Whenever using [Element:setProperty][] to set an attribute, pass in the lowercase, simplified form of the property. For example:
298         - use 'for', not 'htmlFor',
299         - use 'class', not 'className'
300         - use 'frameborder', not 'frameBorder'
301         - etc.
304 ### See Also:
306 - [Element][], [Element.Properties][], [Element:setProperty][], [Element:addEvents][], [Element:setStyles][]
310 Element Method: get {#Element:get}
311 ----------------------------------
313 This is a "dynamic arguments" method. Properties passed in can be any of the 'get' properties in the [Element.Properties][] Hash.
315 ### Syntax:
317         myElement.get(property);
319 ### Arguments:
321 1. property - (*string*) The string key from the [Element.Properties][] Hash representing the property to get.
323 ### Returns:
325 * (*mixed*) The result of calling the corresponding 'get' function in the [Element.Properties][] Hash.
327 ### Examples:
329 #### Using Custom Getters:
331         var tag = $('myDiv').get('tag'); //Returns "div".
333 #### Fallback to Element Attributes:
335         var id = $('myDiv').get('id'); //Returns "myDiv".
336         var value = $('myInput').get('value'); //Returns the myInput element's value.
338 ### Notes:
340 -  If the corresponding accessor doesn't exist in the [Element.Properties][] Hash, the result of [Element:getProperty][] on the property passed in is returned.
342 ### See Also:
344 - [Element][], [Element.Properties][], [Element:getProperty][]
348 Element Method: erase {#Element:erase}
349 --------------------------------------
351 This is a "dynamic arguments" method. Properties passed in can be any of the 'erase' properties in the [Element.Properties][] Hash.
353 ### Syntax:
355         myElement.erase(property);
357 ### Arguments:
359 1. property - (*string*) The string key from the [Element.Properties][] Hash representing the property to erase.
361 ### Returns:
363 * (*mixed*) The result of calling the corresponding 'erase' function in the [Element.Properties][] Hash.
365 ### Examples:
367         $('myDiv').erase('id'); //Removes the id from myDiv.
368         $('myDiv').erase('class'); //myDiv element no longer has any class names set.
370 ### Note:
372 -  If the corresponding eraser doesn't exist in the  [Element.Properties][] Hash, [Element:removeProperty][] is called with the property passed in.
374 ### See Also:
376 - [Element][], [Element.Properties][], [Element:removeProperty][]
380 Element Method: match {#Element:match}
381 --------------------------------------
383 Tests this Element to see if it matches the argument passed in.
385 ### Syntax:
387         myElement.match(match);
389 ### Arguments:
391 1. match - can be a string or element
392         - (*string*) The tag name to test against this element. If [Selectors][] is included, any single CSS selectors may also be passed.
393         - (*element*) An element to match; returns true if this is the actual element passed in.
395 ### Returns:
397 * (*boolean*) If the element matched, returns true. Otherwise, returns false.
399 ### Examples:
401 #### Using a Tag Name:
403         //Returns true if #myDiv is a div.
404         $('myDiv').match('div');
406 #### Using a CSS Selector:
408         //Returns true if #myDiv has the class foo and is named "bar"
409         $('myDiv').match('.foo[name=bar]');
411 #### Using an Element:
413         var el = $('myDiv');
414         $('myDiv').match(el); //Returns true
415         $('otherElement').match(el); //Returns false
419 Element Method: inject {#Element:inject}
420 ----------------------------------------
422 Injects, or inserts, the Element at a particular place relative to the Element's children (specified by the second the argument).
424 ### Syntax:
426         myElement.inject(el[, where]);
428 ### Arguments:
430 1. el   - (*mixed*) el can be the id of an element or an element.
431 2. where - (*string*, optional: defaults to 'bottom') The place to inject this Element.  Can be 'top', 'bottom', 'after', or 'before'.
433 ### Returns:
435 * (*element*) This Element.
437 ### Examples:
439 ##### JavaScript
441         var myFirstElement  = new Element('div', {id: 'myFirstElement'});
442         var mySecondElement = new Element('div', {id: 'mySecondElement'});
443         var myThirdElement  = new Element('div', {id: 'myThirdElement'});
445 ##### Resulting HTML
447         <div id="myFirstElement"></div>
448         <div id="mySecondElement"></div>
449         <div id="myThirdElement"></div>
451 #### Inject to the bottom:
453 ##### JavaScript
455         myFirstElement.inject(mySecondElement);
457 ##### Resulting HTML
459         <div id="mySecondElement">
460                 <div id="myFirstElement"></div>
461         </div>
463 #### Inject to the top:
465 ##### JavaScript
467         myThirdElement.inject(mySecondElement, 'top');
469 ##### Resulting HTML
471         <div id="mySecondElement">
472                 <div id="myThirdElement"></div>
473                 <div id="myFirstElement"></div>
474         </div>
476 #### Inject before:
478 ##### JavaScript
480         myFirstElement.inject(mySecondElement, 'before');
482 ##### Resulting HTML
484         <div id="myFirstElement"></div>
485         <div id="mySecondElement"></div>
487 #### Inject After:
489 ##### JavaScript
491         myFirstElement.inject(mySecondElement, 'after');
493 ##### Resulting HTML
495         <div id="mySecondElement"></div>
496         <div id="myFirstElement"></div>
498 ### See Also:
500 [Element:adopt](#Element:adopt), [Element:grab](#Element:grab), [Element:wraps](#Element:wraps)
504 Element Method: grab {#Element:grab}
505 ------------------------------------
507 Works as [Element:inject](#Element:inject), but in reverse.
509 Appends the Element at a particular place relative to the Element's children (specified by the where parameter).
511 ### Syntax:
513         myElement.grab(el[, where]);
515 ### Arguments:
517 1. el - (*mixed*) el can be the id of an element or an Element.
518 2. where - (*string*, optional: default 'bottom') The place to append this Element. Can be 'top' or 'bottom'.
520 ### Returns:
522 * (*element*) This Element.
524 ### Examples:
526 ##### JavaScript
528         var myFirstElement = new Element('div', {id: 'myFirstElement'});
529         var mySecondElement = new Element('div', {id: 'mySecondElement'});
531         myFirstElement.grab(mySecondElement);
533 ##### Resulting HTML
535         <div id="myFirstElement">
536                 <div id="mySecondElement"></div>
537         </div>
539 ### See Also:
541 [Element:adopt](#Element:adopt), [Element:inject](#Element:inject), [Element:wraps](#Element:wraps)
545 Element Method: adopt {#Element:adopt}
546 --------------------------------------
548 Works like [Element:grab](#Element:grab), but allows multiple elements to be adopted.
550 Inserts the passed element(s) inside the Element (which will then become the parent element).
552 ### Syntax:
554         myParent.adopt(el[, others]);
556 ### Arguments:
558 1. el - (*mixed*) The id of an element, an Element, or an array of elements.
559 2. others - (*mixed*, optional) One or more additional Elements separated by a comma or as an array.
561 ### Returns:
563 * (*element*) This Element.
565 ### Examples:
567 ##### JavaScript
569         var myFirstElement  = new Element('div', {id: 'myFirstElement'});
570         var mySecondElement = new Element('a', {id: 'mySecondElement'});
571         var myThirdElement  = new Element('ul', {id: 'myThirdElement'});
573         myParent.adopt(myFirstElement);
574         myParent2.adopt(myFirstElement, 'mySecondElement');
575         myParent3.adopt([myFirstElement, mySecondElement, myThirdElement]);
577 ##### Resulting HTML
579         <div id="myParent">
580                 <div id="myFirstElement" />
581         </div>
582         <div id="myParent2">
583                 <div id="myFirstElement" />
584                 <a />
585         </div>
586         <div id="myParent3">
587                 <div id="myFirstElement" />
588                 <a />
589                 <ul />
590         </div>
592 ### See Also:
594 [Element:grab](#Element:grab), [Element:inject](#Element:inject), [Element:wraps](#Element:wraps)
598 Element Method: wraps {#Element:wraps}
599 --------------------------------------
601 Works like [Element:grab](#Element:grab), but instead of moving the grabbed element from its place, this method moves this Element around its target.
603 The Element is moved to the position of the passed element and becomes the parent.
605 ### Syntax:
607         myParent.wraps(el[, where]);
609 ### Arguments:
611 1. el - (*mixed*) The id of an element or an Element.
612 2. where - (*string*, optional: default 'bottom') The place to insert the passed in element. Can be 'top' or 'bottom'.
614 ### Returns:
616 * (*element*) This Element.
618 ### Examples:
620 ##### HTML
622         <div id="myFirstElement"></div>
624 ##### JavaScript
626         var mySecondElement = new Element('div', {id: 'mySecondElement'});
627         mySecondElement.wraps($('myFirstElement'));
629 ##### Resulting HTML
631         <div id="mySecondElement">
632                 <div id="myFirstElement"></div>
633         </div>
637 Element Method: appendText {#Element:appendText}
638 ------------------------------------------------
640 Works like [Element:grab](#Element:grab), but instead of accepting an id or an element, it only accepts text.
641 A text node will be created inside this Element, in either the top or bottom position.
643 ### Syntax:
645         myElement.appendText(text);
647 ### Arguments:
649 1. text  - (*string*) The text to append.
650 1. where - (*string*, optional: default 'bottom') The position to inject the text to.
652 ### Returns:
654 * (*element*) The current Element instance.
656 ### Examples:
658 ##### HTML
660         <div id="myElement">Hey.</div>
662 ##### JavaScript
664         $('myElement').appendText(' Howdy.');
666 ##### Resulting HTML
668         <div id="myElement">Hey. Howdy.</div>
672 Element Method: dispose {#Element:dispose}
673 ------------------------------------------
675 Removes the Element from the DOM.
678 ### Syntax:
680         var removedElement = myElement.dispose();
682 ### Returns:
684 * (*element*) This Element. Useful to always grab the return from this function, as the element could be [injected](#Element:inject) back.
686 ### Examples:
688 ##### HTML
690         <div id="myElement"></div>
691         <div id="mySecondElement"></div>
693 ##### JavaScript
695         $('myElement').dispose();
697 ##### Resulting HTML
699         <div id="mySecondElement"></div>
701 ### See Also:
703 - [MDC Element:removeChild](http://developer.mozilla.org/en/docs/DOM:element.removeChild)
707 Element Method: clone {#Element:clone}
708 --------------------------------------
710 Clones the Element and returns the cloned one.
713 ### Syntax:
715         var copy = myElement.clone([contents, keepid]);
717 ### Arguments:
719 1. contents - (*boolean*, optional: defaults to true) When set to false the Element's contents are not cloned.
720 2. keepid - (*boolean*, optional: defaults to false) When true the cloned Element keeps the id attribute, if present. Same goes for any of the cloned childNodes.
723 ### Returns:
725 * (*element*) The cloned Element.
727 ### Examples:
729 ##### HTML
731         <div id="myElement"></div>
733 ##### JavaScript
735         //Clones the Element and appends the clone after the Element.
736         var clone = $('myElement').clone().injectAfter('myElement');
738 ##### Resulting HTML
740         <div id="myElement">ciao</div>
741         <div>ciao</div>
743 ### Note:
745 - The returned Element does not have attached events. To clone the events use [Element:cloneEvents](/Element/Element.Event#Element:cloneEvents).
746 - Values stored in Element.Storage are not cloned.
747 - The clone element and its children are stripped of ids, unless otherwise specified by the keepid parameter.
749 ### See Also:
751 - [Element:cloneEvents](/Element/Element.Event#Element:cloneEvents).
755 Element Method: replaces {#Element:replaces}
756 --------------------------------------------------
758 Replaces the Element with an Element passed.
760 ### Syntax:
762         var element = myElement.replaces(el);
764 ### Arguments:
766 1. el - (*mixed*) A string id representing the Element to be replaced with, or an Element reference.
768 ### Returns:
770 * (*element*) This Element.
772 ### Examples:
774         $('myNewElement').replaces($('myOldElement'));
775         //$('myOldElement') is gone, and $('myNewElement') is in its place.
777 ### See Also:
779 - [MDC Element:replaceChild](http://developer.mozilla.org/en/docs/DOM:element.replaceChild)
783 Element Method: hasClass {#Element:hasClass}
784 --------------------------------------------
786 Tests the Element to see if it has the passed in className.
788 ### Syntax:
790         var result = myElement.hasClass(className);
792 ### Arguments:
794 1. className - (*string*) The class name to test.
796 ### Returns:
798 * (*boolean*) Returns true if the Element has the class, otherwise false.
800 ### Examples:
802 ##### HTML
804         <div id="myElement" class="testClass"></div>
806 ##### JavaScript
808         $('myElement').hasClass('testClass'); //returns true
812 Element Method: addClass {#Element:addClass}
813 --------------------------------------------
815 Adds the passed in class to the Element, if the Element doesnt already have it.
817 ### Syntax:
819         myElement.addClass(className);
821 ### Arguments:
823 1. className - (*string*) The class name to add.
825 ### Returns:
827 * (*element*) This Element.
829 ### Examples:
831 ##### HTML
833         <div id="myElement" class="testClass"></div>
835 ##### JavaScript
837         $('myElement').addClass('newClass');
839 ##### Resulting HTML
841         <div id="myElement" class="testClass newClass"></div>
845 Element Method: removeClass {#Element:removeClass}
846 ----------------------------
848 Works like [Element:addClass](#Element:addClass), but removes the class from the Element.
851 ### Syntax:
853         myElement.removeClass(className);
855 ### Arguments:
857 1. className - (*string*) The class name to remove.
859 ### Returns:
861 * (*element*) This Element.
863 ### Examples:
865 ##### HTML
867         <div id="myElement" class="testClass newClass"></div>
869 ##### JavaScript
871         $('myElement').removeClass('newClass');
873 ##### Resulting HTML
875         <div id="myElement" class="testClass"></div>
879 Element Method: toggleClass {#Element:toggleClass}
880 --------------------------------------------------
882 Adds or removes the passed in class name to the Element, depending on whether or not it's already present.
884 ### Syntax:
886         myElement.toggleClass(className);
888 ### Arguments:
890 1. className - (*string*) The class to add or remove.
892 ### Returns:
894 * (*element*) This Element.
896 ### Examples:
898 ##### HTML
900         <div id="myElement" class="myClass"></div>
902 ##### JavaScript
904         $('myElement').toggleClass('myClass');
906 ##### Resulting HTML
908         <div id="myElement" class=""></div>
910 ##### JavaScript
912         $('myElement').toggleClass('myClass');
914 ##### Resulting HTML
916         <div id="myElement" class="myClass"></div>
920 Element Method: getPrevious {#Element:getPrevious}
921 --------------------------------------------------
923 Returns the previousSibling of the Element (excluding text nodes).
925 ### Syntax:
927         var previousSibling = myElement.getPrevious([match]);
929 ### Arguments:
931 1. match - (*string*, optional): A tag name to match the the found element(s) with. If [Selectors][] is included, a full CSS selector can be passed.
933 ### Returns:
935 * (*mixed*) The previous sibling Element or null if none found.
939 Element Method: getAllPrevious {#Element:getAllPrevious}
940 --------------------------------------------------------
942 Like [Element:getPrevious][], but returns a collection of all the matched previousSiblings.
946 Element Method: getNext {#Element:getNext}
947 ------------------------------------------
949 As [Element:getPrevious][], but tries to find the nextSibling (excluding text nodes).
952 ### Syntax:
954         var nextSibling = myElement.getNext([match]);
956 ### Arguments:
958 1. match - (*string*, optional): A comma seperated list of tag names to match the found element(s) with. If [Selectors][] is included, a full CSS selector can be passed.
960 ### Returns:
962 * (*mixed*) The next sibling Element or null if none found.
965 Element Method: getAllNext {#Element:getAllNext}
966 ------------------------------------------------
968 Like Element.getNext, but returns a collection of all the matched nextSiblings.
972 Element Method: getFirst {#Element:getFirst}
973 --------------------------------------------
975 Works as [Element:getPrevious][], but tries to find the firstChild (excluding text nodes).
978 ### Syntax:
980         var firstElement = myElement.getFirst([match]);
982 ### Arguments:
984 1. match - (*string*, optional): A tag name to match the found element(s) with. if [Selectors][] is included, a full CSS selector can be passed.
986 ### Returns:
988 * (*mixed*) The first sibling Element or null if none found.
992 Element Method: getLast {#Element:getLast}
993 ------------------------------------------
995 Works as [Element:getPrevious][], but tries to find the lastChild.
997 ### Syntax:
999         var lastElement = myElement.getLast([match]);
1001 ### Arguments:
1003 1. match - (*string*, optional): A tag name to match the found element(s) with. if [Selectors][] is included, a full CSS selector can be passed.
1005 ### Returns:
1007 * (*mixed*) The first sibling Element, or returns null if none found.
1011 Element Method: getParent {#Element:getParent}
1012 ----------------------------------------------
1014 Works as [Element:getPrevious][], but tries to find the parentNode.
1017 ### Syntax:
1019         var parent = myElement.getParent([match]);
1021 ### Arguments:
1023 1. match - (*string*, optional): A tag name to match the found element(s) with. if [Selectors][] is included, a full CSS selector can be passed.
1025 ### Returns:
1027 * (*mixed*) The target Element's parent or null if no matching parent is found.
1031 Element Method: getParents {#Element:getParents}
1032 ------------------------------------------------
1034 Like [Element:getParent](#Element:getParent), but returns a collection of all the matched parentNodes up the tree.
1038 Element Method: getChildren {#Element:getChildren}
1039 --------------------------------------------------
1041 Returns all the Element's children (excluding text nodes). Returns as [Elements][].
1044 ### Syntax:
1046         var children = myElement.getChildren([match]);
1048 ### Arguments:
1050 1. match - (*string*, optional): A tag name to match the found element(s) with. if [Selectors][] is included, a full CSS selector can be passed.
1052 ### Returns:
1054 * (*array*) A [Elements](#Elements) array with all of the Element's children, except the text nodes.
1058 Element Method: hasChild {#Element:hasChild}
1059 --------------------------------------------
1061 Checks all descendants of this Element for a match.
1064 ### Syntax:
1066         var result = myElement.hasChild(el);
1068 ### Arguments:
1070 1. el - (*mixed*) Can be an Element reference or string id.
1072 ### Returns:
1074 * (*boolean*) Returns true if the passed in Element is a child of the Element, otherwise false.
1076 ### Examples:
1078 ##### HTML
1080         <div id="Darth_Vader">
1081                 <div id="Luke"></div>
1082         </div>
1084 ##### JavaScript
1086         if ($('Darth_Vader').hasChild('Luke')) alert('Luke, I am your father.'); // tan tan tannn...
1090 Element Method: empty {#Element:empty}
1091 --------------------------------------
1093 Empties an Element of all its children.
1096 ### Syntax:
1098         myElement.empty();
1100 ### Returns:
1102 * (*element*) This Element.
1104 ### Examples:
1106 ##### HTML
1108         <div id="myElement">
1109                 <p></p>
1110                 <span></span>
1111         </div>
1113 ##### JavaScript
1115         $('myElement').empty();
1117 ##### Resulting HTML
1119         <div id="myElement"></div>
1123 Element Method: destroy {#Element:destroy}
1124 ------------------------------------------
1126 Empties an Element of all its children, removes and garbages the Element.
1127 Useful to clear memory before the pageUnload.
1129 ### Syntax:
1131         myElement.destroy();
1133 ### Returns:
1135 * (*null*)
1139 Element Method: toQueryString {#Element:toQueryString}
1140 ------------------------------------------------------
1142 Reads the child inputs of the Element and generates a query string based on their values.
1145 ### Syntax:
1147         var query = myElement.toQueryString();
1149 ### Returns:
1151 * (*string*) A string representation of a all the input Elements' names and values.
1153 ### Examples:
1155 ##### HTML
1157         <form id="myForm" action="submit.php">
1158                 <input name="email" value="bob@bob.com" />
1159                 <input name="zipCode" value="90210" />
1160         </form>
1162 ##### JavaScript
1164         $('myForm').toQueryString(); //Returns "email=bob@bob.com&zipCode=90210".
1167 Element Method: getSelected {#Element:getSelected}
1168 --------------------------------------------------
1170 Returns the selected options of a select element.
1173 ### Syntax:
1175         var selected = mySelect.getSelected();
1177 ### Returns:
1179 * (*array*) An array of the selected elements.
1181 ### Examples:
1183 ##### HTML
1185         <select id="country-select" name="country">
1186                 <option value="US">United States</option
1187                 <option value ="IT">Italy</option>
1188         </select>
1190 ##### JavaScript
1192         $('country-select').getSelected(); //Returns whatever the user selected.
1194 ### Note:
1196 This method returns an array, regardless of the multiple attribute of the select element.
1197 If the select is single, it will return an array with only one item.
1201 Element Method: getProperty {#Element:getProperty}
1202 --------------------------------------------------
1204 Returns a single element attribute.
1206 ### Syntax:
1208         var myProp = myElement.getProperty(property);
1210 ### Arguments:
1212 * property - (*string*) The property to be retrieved.
1214 ### Returns:
1216 * (*string*) A string containing the Element's requested property.
1218 ### Examples:
1220 ##### HTML
1222         <img id="myImage" src="mootools.png" title="MooTools, the compact JavaScript framework" alt="" />
1224 ##### JavaScript
1226         var imgProps = $('myImage').getProperty('src'); //Returns: 'mootools.png'.
1230 Element Method: getProperties {#Element:getProperties}
1231 ------------------------------------------------------
1233 Gets multiple element attributes.
1235 ### Syntax:
1237         var myProps = myElement.getProperties(properties);
1239 ### Arguments:
1241 * properties - (*strings*) Any number of properties to be retrieved.
1243 ### Returns:
1245 * (*object*) An object containing all of the Element's requested properties.
1247 ### Examples:
1249 ##### HTML
1251         <img id="myImage" src="mootools.png" title="MooTools, the compact JavaScript framework" alt="" />
1253 ##### JavaScript
1255         var imgProps = $('myImage').getProperties('id', 'src', 'title', 'alt');
1256         //Returns: { id: 'myImage', src: 'mootools.png', title: 'MooTools, the compact JavaScript framework', alt: '' }
1260 Element Method: setProperty {#Element:setProperty}
1261 --------------------------------------------------
1263 Sets an attribute or special property for this Element.
1266 ### Arguments:
1268 1. property - (*string*) The property to assign the value passed in.
1269 2. value - (*mixed*) The value to assign to the property passed in.
1271 ### Returns:
1273 * (*element*) - This Element.
1275 ### Examples:
1277 ##### HTML
1279         <img id="myImage" />
1281 ##### JavaScript
1283         $('myImage').setProperty('src', 'mootools.png');
1285 ##### Resulting HTML
1287         <img id="myImage" src="mootools.png" />
1289 ### Note
1291 - Whenever using [Element:setProperty][] to set an attribute, pass in the lowercase, simplified form of the property. For example:
1292         - use 'for', not 'htmlFor',
1293         - use 'class', not 'className'
1294         - use 'frameborder', not 'frameBorder'
1295         - etc.
1298 Element Method: setProperties {#Element:setProperties}
1299 ------------------------------------------------------
1301 Sets numerous attributes for the Element.
1304 ### Arguments:
1306 1. properties - (*object*) An object with key/value pairs.
1308 ### Returns:
1310 * (*element*) This Element.
1312 ### Examples:
1314 ##### HTML
1316         <img id="myImage" />
1318 ##### JavaScript
1320         $('myImage').setProperties({
1321                 src: 'whatever.gif',
1322                 alt: 'whatever dude'
1323         });
1325 ##### Resulting HTML
1327         <img id="myImage" src="whatever.gif" alt="whatever dude" />
1331 Element Method: removeProperty {#Element:removeProperty}
1332 --------------------------------------------------------
1334 Removes an attribute from the Element.
1337 ### Syntax:
1339         myElement.removeProperty(property);
1341 ### Arguments:
1343 1. property - (*string*) The attribute to remove.
1345 ### Returns:
1347 * (*element*) This Element.
1349 ### Examples:
1351 ##### HTML
1353         <a id="myAnchor" href="#" onmousedown="alert('click');"></a>
1355 ##### JavaScript
1357         //Eww... inline JavaScript is bad! Let's get rid of it.
1358         $('myAnchor').removeProperty('onmousedown');
1360 ##### Resulting HTML
1362         <a id="myAnchor" href="#"></a>
1366 Element Method: removeProperties {#Element:removeProperties}
1367 ------------------------------------------------------------
1369 Removes numerous attributes from the Element.
1372 ### Syntax:
1374         myElement.removeProperties(properties);
1376 ### Arguments:
1378 1. properties - (*strings*) The attributes to remove, separated by comma.
1380 ### Returns:
1382 * (*element*) This Element.
1384 ### Examples:
1386 ##### HTML
1388         <a id="myAnchor" href="#" title="hello world"></a>
1390 ##### JavaScript
1392         $('myAnchor').removeProperties('id', 'href', 'title');
1394 ##### Resulting HTML
1396         <a></a>
1399 Element Method: store {#Element:store}
1400 --------------------------------------
1402 Stores an item in the Elements Storage, linked to this Element.
1405 ### Syntax:
1407         myElement.store(key, value);
1409 ### Arguments:
1411 1. key - (*string*) The key you want to assign to the stored value.
1412 2. value - (*mixed*) Any value you want to store.
1414 ### Returns:
1416 * (*element*) This Element.
1418 ### Example:
1420         $('element').store('someProperty', someValue);
1423 Element Method: retrieve {#Element:retrieve}
1424 --------------------------------------------
1426 Retrieves a value from the Elements storage.
1429 ### Syntax:
1431         myElement.retrieve(key[, default]);
1433 ### Arguments:
1435 1. key - (*string*) The key you want to retrieve from the storage.
1436 2. default - (*mixed*, optional) Default value to store and return if no value is stored.
1438 ### Returns:
1440 * (*mixed*) The value linked to the key.
1442 ### Example:
1444         $('element').retrieve('someProperty'); // returns someValue (see example above)
1447 Element Method: eliminate {#Element:eliminate}
1448 --------------------------------------------
1450 Eliminates a key from the Elements storage.
1453 ### Syntax:
1455         myElement.retrieve(key);
1457 ### Arguments:
1459 1. key - (*string*) The key you want to eliminate from the storage.
1461 ### Returns:
1463 * (*mixed*) The element/window/document.
1465 ### Example:
1467         $('element').eliminate('someProperty');
1473 Hash: Element.Properties {#Element-Properties}
1474 ==============================================
1476 This Hash contains the functions that respond to the first argument passed in [Element:get][], [Element:set][] and [Element:erase][].
1478 ### Adding a Custom Element Property
1480         Element.Properties.disabled = {
1482                 get: function(){
1483                         return this.disabled;
1484                 }
1486                 set: function(value){
1487                         this.disabled = !!value;
1488                         this.setAttribute('disabled', !!value);
1489                 }
1491         };
1493 ### Using a Custom Element Property
1495         //Gets the "disabled" property.
1496         $(element).get('disabled');
1497         //Sets the "disabled" property to true, along with the attribute.
1498         $(element).set('disabled', true);
1500 ### Note:
1502 Automatically returns the element for setters.
1504 ### Using an Object:
1506 Additionally, you can access these custom getters and setters using an object as the parameter for the [set](#Element:set) method.
1508 #### Example:
1510         //Using set:
1511         $(divElement).set({html: '<p>Hello <em>People</em>!</p>', style: 'background:red'});
1513         //For new Elements (works the same as set):
1514         new Element('input', {type: 'checkbox', checked: true, disabled: true});
1518 Element Property: html {#Element-Properties:html}
1519 -------------------------------------------------
1521 ### Setter:
1523 Sets the innerHTML of the Element.
1525 #### Syntax:
1527         myElement.set('html', [htmlString[, htmlString2[, htmlString3[, ..]]]);
1529 #### Arguments:
1531 1. Any number of string parameters with HTML.
1533 #### Returns:
1535 * (*element*) This Element.
1537 #### Examples:
1539 ##### HTML
1541         <div id="myElement"></div>
1543 ##### JavaScript
1545         $('myElement').set('html', '<div></div>', '<p></p>');
1547 ##### Resulting HTML
1549         <div id="myElement">
1550                 <div></div>
1551                 <p></p>
1552         </div>
1554 ### Getter:
1556 Returns the inner HTML of the Element.
1558 #### Syntax:
1560         myElement.get('html');
1562 #### Returns:
1564 * (*text*) This Element's innerHTML.
1568 Element Property: text {#Element-Properties:text}
1569 -------------------------------------------------
1571 ### Setter:
1573 Sets the inner text of the Element.
1575 #### Syntax:
1577         myElement.set('text', text);
1579 #### Arguments:
1581 1. text - (*string*) The new text content for the Element.
1583 #### Returns:
1585 * (*element*) This Element.
1587 #### Examples:
1589 ##### HTML
1591         <div id="myElement"></div>
1593 ##### JavaScript
1595         $('myElement').set('text', 'some text');
1596         //The text of myElement is now 'some text'.
1598 ##### Resulting HTML
1600         <div id="myElement">some text</div>
1602 ### Getter:
1604 Gets the inner text of the Element.
1606 #### Syntax:
1608         var myText = myElement.get('text');
1610 #### Returns:
1612 * (*string*) The text of the Element.
1614 #### Examples:
1616 ##### HTML
1618         <div id="myElement">my text</div>
1620 ##### JavaScript
1622         var myText = $('myElement').get('text'); //myText = 'my text'.
1626 Element Property: tag {#Element-Properties:tag}
1627 -----------------------------------------------
1629 ### Getter:
1631 Returns the tag name of the Element in lower case.
1633 #### Syntax:
1635         var myTag = myElement.get('tag');
1637 #### Returns:
1639 * (*string*) The tag name in lower case.
1641 #### Examples:
1643 ##### HTML
1645         <img id="myImage" />
1647 ##### JavaScript
1649         var myTag = $('myImage').get('tag'); // myTag = 'img'.
1653 Native: IFrame {#IFrame}
1654 ========================
1656 Custom Native to create and easily work with IFrames.
1660 IFrame Method: constructor {#IFrame:constructor}
1661 ------------------------------------------------
1663 Creates an IFrame HTML Element and extends its window and document with MooTools.
1666 ### Syntax:
1668         var myIFrame = new IFrame([el][, props]);
1670 ### Arguments:
1672 1. el - (*mixed*, optional) The id of the IFrame to be converted, or the actual IFrame element. If its not passed, a new IFrame will be created (default).
1673 2. props - (*object*, optional) The properties to be applied to the new IFrame. Same as [Element:constructor](#Element:constructor) props argument.
1675 ### Returns:
1677 * (*element*) A new IFrame HTML Element.
1679 ### Examples:
1681         var myIFrame = new IFrame({
1683                 src: 'http://mootools.net/',
1685                 styles: {
1686                         width: 800,
1687                         height: 600,
1688                         border: '1px solid #ccc'
1689                 },
1691                 events: {
1693                         mouseenter: function(){
1694                                 alert('Welcome aboard.');
1695                         },
1697                         mouseleave: function(){
1698                                 alert('Goodbye!');
1699                         },
1701                         load: function(){
1702                                 alert('The iframe has finished loading.');
1703                         }
1705                 }
1707         });
1710 ### Notes:
1712 - If the IFrame is from the same domain as the "host" or running locally, its document and window will be extended with MooTools functionalities, allowing you to fully use MooTools within it.
1713 - If the IFrame already exists and has a different name than id, the name will be made the same as the id.
1714 - If the IFrame is from a different domain, its window and document will not be extended with MooTools methods.
1718 Native: Elements {#Elements}
1719 ============================
1721 The Elements class allows [Element][] methods to work on an [Elements][] array, as well as [Array][] Methods.
1725 Elements Method: constructor {#Elements:constructor}
1726 ----------------------------------------------------
1729 ### Syntax:
1731         var myElements = new Elements(elements[, options]);
1733 ### Arguments:
1735 1. elements - (*mixed*) An array of elements or an HTMLCollection Object.
1737 ### Returns:
1739 * (*array*) An extended array with the [Element][], [Elements][] and [Array][] methods.
1741 ### Examples:
1743 #### Set Every Paragraph's Color to Red:
1745         $$('p').each(function(el){
1746                 el.setStyle('color', 'red');
1747         });
1749         //Because $$('myselector') also accepts Element methods, the below
1750         //example has the same effect as the one above.
1751         $$('p').setStyle('color', 'red');
1754 #### Create Elements From an Array:
1756         var myElements = new Elements(['myElementID', $('myElement'), 'myElementID2', document.getElementById('myElementID3')]);
1759 ### Notes:
1761 - In MooTools, every DOM function which returns a collection of nodes (such as [$$][]) returns the nodes as instances of Elements.
1762 - Because Elements is an Array, it accepts all the [Array][] methods, while giving precedence to [Element][] and [Elements][] methods.
1763 - Every node of the Elements instance has all the [Element][] methods.
1765 ### See Also:
1767 - [$$][], [$][], [Element][], [Elements][], [Array][]
1771 Elements Method: filter {#Elements:filter}
1772 ----------------------------------------------
1774 Filters a collection of elements by a given tag name.  If [Selectors][] is included, this method will be able to filter by any selector.
1775 It also works like [Array:filter](/Native/Array/#Array:filter), by filtering collection of elements with a function.
1778 ### Syntax:
1780         var filteredElements = elements.filter(selector);
1782 ### Arguments:
1784 1. selector - (*mixed*) A single CSS selector.
1786 ### Returns:
1788 * (*array*) A subset of this [Elements][] instance.
1792 [$]: #dollar
1793 [$$]: #dollars
1795 [Array]: /core/Native/Array
1796 [Selectors]: /core/Utilities/Selectors
1798 [Element]: #Element
1799 [Elements]: #Elements
1800 [Element:set]: #Element:set
1801 [Element:get]: #Element:get
1802 [Element:erase]: #Element:erase
1803 [Element:setProperty]: #Element:setProperty
1804 [Element:getProperty]: #Element:getProperty
1805 [Element:removeProperty]: #Element:removeProperty
1806 [Element:getElement]: #Element:getElement
1807 [Element:getElements]: #Element:getElements
1808 [Element.Properties]: #Element-Properties
1809 [Element:getPrevious]: #Element:getPrevious
1811 [Element:addEvents]: /core/Element/Element.Event#Element:addEvents
1812 [Element:setStyles]: /core/Element/Element.Style#Element:setStyles