- MooTools - Fx.Events
- MooTools - Fx.Options
- MooTools - Fx.Morph
- MooTools - Fx.Tween
- MooTools - Fx.Slide
- MooTools - Fx.Element
- MooTools - Classes
- MooTools - Tabbed Content
- MooTools - Tooltips
- MooTools - Accordion
- MooTools - Sortables
- MooTools - Sliders
- MooTools - Periodicals
- MooTools - Regular Expression
- MooTools - Drag and Drop
- MooTools - Input Filtering
- MooTools - Style Properties
- MooTools - DOM Manipulations
- MooTools - Event Handling
- MooTools - Functions
- MooTools - Using Arrays
- MooTools - Selectors
- MooTools - Program Structure
- MooTools - Installation
- MooTools - Introduction
- MooTools - Home
MooTools Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MooTools - DOM Manipulations
We already know that every HTML page is designed using DOM elements. Using MooTools you can manipulate DOM elements which means you can create, remove and change the style of DOM elements.
Basic methods
The following are the basic methods that capture and help to modify the properties of the DOM elements.
get()
This method is used to retrieve the element properties such as src, value, name, etc. The following statement is the syntax of the get method.
Syntax
//this will return the html tag (span, a, span...) of the element $( id_name ).get( tag );
You will receive the following pst of properties while retrieving the element using the get() method.
id
name
value
href
src
class (will return all classes if the element)
text (the text content of an element)
set()
This method is used to set a value to a variable. This is useful when combined with events and lets you change values. The following statement is the syntax of the set method.
Syntax
//this will set the href of #id_name to "http://www.google.com" $( id_name ).set( href , http://www.google.com );
erase()
This method helps you erase the value of an elements property. You need to choose which property you want to erase from the element. The following statement is the syntax of the erase() method.
Syntax
//this will erase the href value of #id_name $( id_name ).erase( href );
Moving Elements
Moving element means moving an existing element from one position to another position around the page. You can use the inject() method to move an element around the page. Let us take an example wherein, one HTML page contains three span elements which contains the content A, B, and C respectively in an order. Take a look at the following code.
Example
<!DOCTYPE html> <html> <head> <script type = "text/javascript" src = "MooTools-Core-1.6.0.js"></script> <script type = "text/javascript" src = "MooTools-More-1.6.0.js"></script> <script type = "text/javascript"> window.addEvent( domready , function() { var elementA = $( elemA ); var elementB = $( elemB ); var elementC = $( elemC ); }) </script> </head> <body> <span id = "body_wrap"> <span id = "elemA">A</span> <span id = "elemB">B</span> <span id = "elemC">C</span> </span> </body> </html>
You will receive the following output −
Output
Now, using the inject() method in MooTools, we can change the order from ABC to ACB. This means, we need to place elementB after elementC and place the elementC before elementB. Take a look at the following code.
Example
<!DOCTYPE html> <html> <head> <script type = "text/javascript" src = "MooTools-Core-1.6.0.js"></script> <script type = "text/javascript" src = "MooTools-More-1.6.0.js"></script> <script type = "text/javascript"> window.addEvent( domready , function() { var elementA = $( elemA ); var elementB = $( elemB ); var elementC = $( elemC ); //translates to: inject element C before element B elementC.inject(elementB, before ); //translates to: inject element B after element C elementB.inject(elementC, after ); }); </script> </head> <body> <span id = "body_wrap"> <span id = "elemA">A</span> <span id = "elemB">B</span> <span id = "elemC">C</span> </span> </body> </html>
You will receive the following output −
Output
Create New Element
MooTools provides an option to create any type of DOM element and insert it into the HTML page. But, we have to maintain a proper syntax for every element. Let us take an example wherein, the following code snippet is the syntax for creating an (anchor) element.
Syntax
var el = new Element( a , { id: Awesome , title: Really? , text: I m awesome , href: http://MooTools.net , events: { cpck : function(e) { e.preventDefault(); alert( Yes, really. ); } }, styles: { color: #f00 } });
Let us take an example that will create an anchor element using MooTools pbrary. Take a look at the following code.
Example
<!DOCTYPE html> <html> <head> <script type = "text/javascript" src = "MooTools-Core-1.6.0.js"></script> <script type = "text/javascript" src = "MooTools-More-1.6.0.js"></script> <script type = "text/javascript"> window.addEvent( domready , function() { var el = new Element( a , { id: Awesome , title: Really? , text: I m awesome , href: http://www.tutorialspoint.com , events: { cpck : function(e) { e.preventDefault(); alert( Yes, really. ); } }, styles: { color: #f00 } }); el.inject(document.body); }); </script> </head> <body> </body> </html>
You will receive the following output −