Contains useful Element prototypes, to be used with the dollar function $.
MIT-style license.
| Element.js | Contains useful Element prototypes, to be used with the dollar function $. |
| Element | Custom class to allow all of its methods to be used with any DOM element via the dollar function $. |
| Properties | |
| initialize | Creates a new element of the type passed in. |
| Utility Functions | |
| Functions | |
| $ | returns the element passed in with all the Element prototypes applied. |
| $$ | Selects, and extends DOM elements. |
| Element | Custom class to allow all of its methods to be used with any DOM element via the dollar function $. |
| Properties | |
| injectBefore | Inserts the Element before the passed element. |
| injectAfter | Same as Element.injectBefore, but inserts the element after. |
| injectInside | Same as Element.injectBefore, but inserts the element inside. |
| adopt | Inserts the passed element inside the Element. |
| remove | Removes the Element from the DOM. |
| clone | Clones the Element and returns the cloned one. |
| replaceWith | Replaces the Element with an element passed. |
| appendText | Appends text node to a DOM element. |
| hasClass | Tests the Element to see if it has the passed in className. |
| addClass | Adds the passed in class to the Element, if the element doesnt already have it. |
| removeClass | works like Element.addClass, but removes the class from the element. |
| toggleClass | Adds or removes the passed in class name to the element, depending on if it’s present or not. |
| setStyle | Sets a css property to the Element. |
| setStyles | Applies a collection of styles to the Element. |
| setOpacity | Sets the opacity of the Element, and sets also visibility == “hidden” if opacity == 0, and visibility = “visible” if opacity > 0. |
| getStyle | Returns the style of the Element given the property passed in. |
| getStyles | Returns an object of styles of the Element for each argument passed in. |
| addEvent | Attaches an event listener to a DOM element. |
| removeEvent | Works as Element.addEvent, but instead removes the previously added event listener. |
| removeEvents | removes all events of a certain type from an element. |
| fireEvent | executes all events of the specified type present in the element. |
| getPrevious | Returns the previousSibling of the Element, excluding text nodes. |
| getNext | Works as Element.getPrevious, but tries to find the nextSibling. |
| getFirst | Works as Element.getPrevious, but tries to find the firstChild. |
| getLast | Works as Element.getPrevious, but tries to find the lastChild. |
| getParent | returns the $(element.parentNode) |
| getChildren | returns all the $(element.childNodes), excluding text nodes. |
| setProperty | Sets an attribute for the Element. |
| setProperties | Sets numerous attributes for the Element. |
| setHTML | Sets the innerHTML of the Element. |
| getProperty | Gets the an attribute of the Element. |
| getTag | Returns the tagName of the element in lower case. |
| scrollTo | scrolls the element to the specified coordinated (if the element has an overflow) |
| getValue | Returns the value of the Element, if its tag is textarea, select or input. |
| getSize | return an Object representing the size/scroll values of the element. |
| getPosition | Returns the real offsets of the element. |
| getTop | Returns the distance from the top of the window to the Element. |
| getLeft | Returns the distance from the left of the window to the Element. |
| getCoordinates | Returns an object with width, height, left, right, top, and bottom, representing the values of the Element |
Custom class to allow all of its methods to be used with any DOM element via the dollar function $.
| Properties | |
| initialize | Creates a new element of the type passed in. |
Creates a new element of the type passed in.
| el | the tag name for the element you wish to create. you can also pass in an element reference, in which case it will be extended. |
| props | an object, the properties you want to add to your element. |
the key styles will be used as setStyles, the key events will be used as addEvents. any other key is used as setProperty.
new Element('a', {
'styles': {
'display': 'block',
'border': '1px solid black'
},
'events': {
'click': function(){
//aaa
},
'mousedown': function(){
//aaa
}
},
'class': 'myClassSuperClass',
'href': 'http://mad4milk.net'
});
function $( el )
returns the element passed in with all the Element prototypes applied.
| el | a reference to an actual element or a string representing the id of an element |
$('myElement') // gets a DOM element by id with all the Element prototypes applied.
var div = document.getElementById('myElement');
$(div) //returns an Element also with all the mootools extentions applied.You’ll use this when you aren’t sure if a variable is an actual element or an id, as well as just shorthand for document.getElementById().
a DOM element or false (if no id was found).
you need to call $ on an element only once to get all the prototypes. But its no harm to call it multiple times, as it will detect if it has been already extended.
function $$()
Selects, and extends DOM elements.
HTMLCollection(document.getElementsByTagName, element.childNodes), an array of elements, a string.
if you loaded Dom.js, $$ will also accept CSS Selectors.
$$('a') //an array of all anchor tags on the page
$$('a', 'b') //an array of all anchor and bold tags on the page
$$('#myElement') //array containing only the element with id = myElement. (only with <Dom.js>)
$$('#myElement a.myClass') //an array of all anchor tags with the class "myClass" within the DOM element with id "myElement" (only with <Dom.js>)| array | array of all the dom elements matched |
Custom class to allow all of its methods to be used with any DOM element via the dollar function $.
| Properties | |
| injectBefore | Inserts the Element before the passed element. |
| injectAfter | Same as Element.injectBefore, but inserts the element after. |
| injectInside | Same as Element.injectBefore, but inserts the element inside. |
| adopt | Inserts the passed element inside the Element. |
| remove | Removes the Element from the DOM. |
| clone | Clones the Element and returns the cloned one. |
| replaceWith | Replaces the Element with an element passed. |
| appendText | Appends text node to a DOM element. |
| hasClass | Tests the Element to see if it has the passed in className. |
| addClass | Adds the passed in class to the Element, if the element doesnt already have it. |
| removeClass | works like Element.addClass, but removes the class from the element. |
| toggleClass | Adds or removes the passed in class name to the element, depending on if it’s present or not. |
| setStyle | Sets a css property to the Element. |
| setStyles | Applies a collection of styles to the Element. |
| setOpacity | Sets the opacity of the Element, and sets also visibility == “hidden” if opacity == 0, and visibility = “visible” if opacity > 0. |
| getStyle | Returns the style of the Element given the property passed in. |
| getStyles | Returns an object of styles of the Element for each argument passed in. |
| addEvent | Attaches an event listener to a DOM element. |
| removeEvent | Works as Element.addEvent, but instead removes the previously added event listener. |
| removeEvents | removes all events of a certain type from an element. |
| fireEvent | executes all events of the specified type present in the element. |
| getPrevious | Returns the previousSibling of the Element, excluding text nodes. |
| getNext | Works as Element.getPrevious, but tries to find the nextSibling. |
| getFirst | Works as Element.getPrevious, but tries to find the firstChild. |
| getLast | Works as Element.getPrevious, but tries to find the lastChild. |
| getParent | returns the $(element.parentNode) |
| getChildren | returns all the $(element.childNodes), excluding text nodes. |
| setProperty | Sets an attribute for the Element. |
| setProperties | Sets numerous attributes for the Element. |
| setHTML | Sets the innerHTML of the Element. |
| getProperty | Gets the an attribute of the Element. |
| getTag | Returns the tagName of the element in lower case. |
| scrollTo | scrolls the element to the specified coordinated (if the element has an overflow) |
| getValue | Returns the value of the Element, if its tag is textarea, select or input. |
| getSize | return an Object representing the size/scroll values of the element. |
| getPosition | Returns the real offsets of the element. |
| getTop | Returns the distance from the top of the window to the Element. |
| getLeft | Returns the distance from the left of the window to the Element. |
| getCoordinates | Returns an object with width, height, left, right, top, and bottom, representing the values of the Element |
Inserts the Element before the passed element.
| el | a string representing the element to be injected in (myElementId, or div), or an element reference. If you pass div or another tag, the element will be created. |
html:
<div id="myElement"></div>
<div id="mySecondElement"></div>
js:
$('mySecondElement').injectBefore('myElement');
resulting html:
<div id="mySecondElement"></div>
<div id="myElement"></div>
Same as Element.injectBefore, but inserts the element after.
Same as Element.injectBefore, but inserts the element inside.
Inserts the passed element inside the Element. Works as Element.injectInside but in reverse.
| el | a string representing the element to be injected in (myElementId, or div), or an element reference. If you pass div or another tag, the element will be created. |
Clones the Element and returns the cloned one.
the cloned element
var clone = $('myElement').clone().injectAfter('myElement');
//clones the Element and append the clone after the Element.Replaces the Element with an element passed.
| el | a string representing the element to be injected in (myElementId, or div), or an element reference. If you pass div or another tag, the element will be created. |
the passed in element
$('myOldElement').replaceWith($('myNewElement')); //$('myOldElement') is gone, and $('myNewElement') is in its place.Appends text node to a DOM element.
| text | the text to append. |
<div id="myElement">hey</div>
$('myElement').appendText(' howdy'); //myElement innerHTML is now "hey howdy"
Tests the Element to see if it has the passed in className.
| true | the Element has the class |
| false | it doesn’t Arguments: |
| className | the class name to test. Example: |
<div id="myElement" class="testClass"></div>
$('myElement').hasClass('testClass'); //returns true
Adds the passed in class to the Element, if the element doesnt already have it.
| className | the class name to add |
<div id="myElement" class="testClass"></div>
$('myElement').addClass('newClass'); //<div id="myElement" class="testClass newClass"></div>
works like Element.addClass, but removes the class from the element.
Adds or removes the passed in class name to the element, depending on if it’s present or not.
| className | the class to add or remove |
<div id="myElement" class="myClass"></div>
$('myElement').toggleClass('myClass');
<div id="myElement" class=""></div>
$('myElement').toggleClass('myClass');
<div id="myElement" class="myClass"></div>
Sets a css property to the Element.
| property | the property to set |
| value | the value to which to set it |
$('myElement').setStyle('width', '300px'); //the width is now 300pxApplies a collection of styles to the Element.
| source | an object or string containing all the styles to apply. You cannot set the opacity using a string. |
$('myElement').setStyles({
border: '1px solid #000',
width: '300px',
height: '400px'
});OR
$('myElement').setStyles('border: 1px solid #000; width: 300px; height: 400px;');Sets the opacity of the Element, and sets also visibility == “hidden” if opacity == 0, and visibility = “visible” if opacity > 0.
| opacity | Accepts numbers from 0 to 1. |
$('myElement').setOpacity(0.5) //make it 50% transparentReturns the style of the Element given the property passed in.
| property | the css style property you want to retrieve |
$('myElement').getStyle('width'); //returns "400px"
//but you can also use
$('myElement').getStyle('width').toInt(); //returns "400"the style as a string
Returns an object of styles of the Element for each argument passed in. Arguments: properties - any number of style properties Example:
$('myElement').getStyles('width','height','padding');
//returns an object like:
{width: "10px", height: "10px", padding: "10px 0px 10px 0px"}Attaches an event listener to a DOM element.
| type | the event to monitor (‘click’, ‘load’, etc) without the prefix ‘on’. |
| fn | the function to execute |
$('myElement').addEvent('click', function(){alert('clicked!')});removes all events of a certain type from an element. if no argument is passed in, removes all events.
Returns the previousSibling of the Element, excluding text nodes.
$('myElement').getPrevious(); //get the previous DOM element from myElementthe sibling element or undefined if none found.
Works as Element.getPrevious, but tries to find the firstChild.
Works as Element.getPrevious, but tries to find the lastChild.
returns all the $(element.childNodes), excluding text nodes. Returns as Elements.
Sets an attribute for the Element.
| property | the property to assign the value passed in |
| value | the value to assign to the property passed in |
$('myImage').setProperty('src', 'whatever.gif'); //myImage now points to whatever.gif for its sourceSets numerous attributes for the Element.
| source | an object with key/value pairs. |
$('myElement').setProperties({
src: 'whatever.gif',
alt: 'whatever dude'
});
<img src="whatever.gif" alt="whatever dude">Sets the innerHTML of the Element.
| html | the new innerHTML for the element. |
$('myElement').setHTML(newHTML) //the innerHTML of myElement is now = newHTMLGets the an attribute of the Element.
| property | the attribute to retrieve |
$('myImage').getProperty('src') // returns whatever.gifthe value, or an empty string
Returns the tagName of the element in lower case.
$('myImage').getTag() // returns 'img'The tag name in lower case
scrolls the element to the specified coordinated (if the element has an overflow)
| x | the x coordinate |
| y | the y coordinate |
$('myElement').scrollTo(0, 100)Returns the value of the Element, if its tag is textarea, select or input. getValue called on a multiple select will return an array.
return an Object representing the size/scroll values of the element.
$('myElement').getSize();{
'scroll': {'x': 100, 'y': 100},
'size': {'x': 200, 'y': 400},
'scrollSize': {'x': 300, 'y': 500}
}returns the element passed in with all the Element prototypes applied.
function $( el )
Selects, and extends DOM elements.
function $$()