Element.js

Contains useful Element prototypes, to be used with the dollar function $.

Authors

License

MIT-style license.

Credits

  • Some functions are inspired by those found in prototype.js http://prototype.conio.net/ © 2005 Sam Stephenson sam [at] conio [doft] net, MIT-style license
Summary
Element.jsContains useful Element prototypes, to be used with the dollar function $.
ElementCustom class to allow all of its methods to be used with any DOM element via the dollar function $.
Properties
initializeCreates 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.
ElementCustom class to allow all of its methods to be used with any DOM element via the dollar function $.
Properties
injectBeforeInserts the Element before the passed element.
injectAfterSame as Element.injectBefore, but inserts the element after.
injectInsideSame as Element.injectBefore, but inserts the element inside.
adoptInserts the passed element inside the Element.
removeRemoves the Element from the DOM.
cloneClones the Element and returns the cloned one.
replaceWithReplaces the Element with an element passed.
appendTextAppends text node to a DOM element.
hasClassTests the Element to see if it has the passed in className.
addClassAdds the passed in class to the Element, if the element doesnt already have it.
removeClassworks like Element.addClass, but removes the class from the element.
toggleClassAdds or removes the passed in class name to the element, depending on if it’s present or not.
setStyleSets a css property to the Element.
setStylesApplies a collection of styles to the Element.
setOpacitySets the opacity of the Element, and sets also visibility == “hidden” if opacity == 0, and visibility = “visible” if opacity > 0.
getStyleReturns the style of the Element given the property passed in.
getStylesReturns an object of styles of the Element for each argument passed in.
addEventAttaches an event listener to a DOM element.
removeEventWorks as Element.addEvent, but instead removes the previously added event listener.
removeEventsremoves all events of a certain type from an element.
fireEventexecutes all events of the specified type present in the element.
getPreviousReturns the previousSibling of the Element, excluding text nodes.
getNextWorks as Element.getPrevious, but tries to find the nextSibling.
getFirstWorks as Element.getPrevious, but tries to find the firstChild.
getLastWorks as Element.getPrevious, but tries to find the lastChild.
getParentreturns the $(element.parentNode)
getChildrenreturns all the $(element.childNodes), excluding text nodes.
setPropertySets an attribute for the Element.
setPropertiesSets numerous attributes for the Element.
setHTMLSets the innerHTML of the Element.
getPropertyGets the an attribute of the Element.
getTagReturns the tagName of the element in lower case.
scrollToscrolls the element to the specified coordinated (if the element has an overflow)
getValueReturns the value of the Element, if its tag is textarea, select or input.
getSizereturn an Object representing the size/scroll values of the element.
getPositionReturns the real offsets of the element.
getTopReturns the distance from the top of the window to the Element.
getLeftReturns the distance from the left of the window to the Element.
getCoordinatesReturns an object with width, height, left, right, top, and bottom, representing the values of the Element

Element

Custom class to allow all of its methods to be used with any DOM element via the dollar function $.

Summary
Properties
initializeCreates a new element of the type passed in.

Properties

initialize

Creates a new element of the type passed in.

Arguments

elthe tag name for the element you wish to create. you can also pass in an element reference, in which case it will be extended.
propsan object, the properties you want to add to your element.

Props

the key styles will be used as setStyles, the key events will be used as addEvents. any other key is used as setProperty.

Example

new Element('a', {
'styles': {
'display': 'block',
'border': '1px solid black'
},
'events': {
'click': function(){
//aaa
},
'mousedown': function(){
//aaa
}
},
'class': 'myClassSuperClass',
'href': 'http://mad4milk.net'
});

Utility Functions

Summary
Functions
$returns the element passed in with all the Element prototypes applied.
$$Selects, and extends DOM elements.

Functions

$

function $(el)

returns the element passed in with all the Element prototypes applied.

Arguments

ela reference to an actual element or a string representing the id of an element

Example

$('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().

Returns

a DOM element or false (if no id was found).

Note

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.

Arguments

HTMLCollection(document.getElementsByTagName, element.childNodes), an array of elements, a string.

Note

if you loaded Dom.js, $$ will also accept CSS Selectors.

Example

$$('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>)

Returns

arrayarray of all the dom elements matched

Element

Custom class to allow all of its methods to be used with any DOM element via the dollar function $.

Summary
Properties
injectBeforeInserts the Element before the passed element.
injectAfterSame as Element.injectBefore, but inserts the element after.
injectInsideSame as Element.injectBefore, but inserts the element inside.
adoptInserts the passed element inside the Element.
removeRemoves the Element from the DOM.
cloneClones the Element and returns the cloned one.
replaceWithReplaces the Element with an element passed.
appendTextAppends text node to a DOM element.
hasClassTests the Element to see if it has the passed in className.
addClassAdds the passed in class to the Element, if the element doesnt already have it.
removeClassworks like Element.addClass, but removes the class from the element.
toggleClassAdds or removes the passed in class name to the element, depending on if it’s present or not.
setStyleSets a css property to the Element.
setStylesApplies a collection of styles to the Element.
setOpacitySets the opacity of the Element, and sets also visibility == “hidden” if opacity == 0, and visibility = “visible” if opacity > 0.
getStyleReturns the style of the Element given the property passed in.
getStylesReturns an object of styles of the Element for each argument passed in.
addEventAttaches an event listener to a DOM element.
removeEventWorks as Element.addEvent, but instead removes the previously added event listener.
removeEventsremoves all events of a certain type from an element.
fireEventexecutes all events of the specified type present in the element.
getPreviousReturns the previousSibling of the Element, excluding text nodes.
getNextWorks as Element.getPrevious, but tries to find the nextSibling.
getFirstWorks as Element.getPrevious, but tries to find the firstChild.
getLastWorks as Element.getPrevious, but tries to find the lastChild.
getParentreturns the $(element.parentNode)
getChildrenreturns all the $(element.childNodes), excluding text nodes.
setPropertySets an attribute for the Element.
setPropertiesSets numerous attributes for the Element.
setHTMLSets the innerHTML of the Element.
getPropertyGets the an attribute of the Element.
getTagReturns the tagName of the element in lower case.
scrollToscrolls the element to the specified coordinated (if the element has an overflow)
getValueReturns the value of the Element, if its tag is textarea, select or input.
getSizereturn an Object representing the size/scroll values of the element.
getPositionReturns the real offsets of the element.
getTopReturns the distance from the top of the window to the Element.
getLeftReturns the distance from the left of the window to the Element.
getCoordinatesReturns an object with width, height, left, right, top, and bottom, representing the values of the Element

Properties

injectBefore

Inserts the Element before the passed element.

Parameteres

ela 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.

Example

html:
<div id="myElement"></div>
<div id="mySecondElement"></div>
js:
$('mySecondElement').injectBefore('myElement');
resulting html:
<div id="mySecondElement"></div>
<div id="myElement"></div>

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.  Works as Element.injectInside but in reverse.

Parameteres

ela 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.

remove

Removes the Element from the DOM.

Example

$('myElement').remove() //bye bye

clone

Clones the Element and returns the cloned one.

Returns

the cloned element

Example

var clone = $('myElement').clone().injectAfter('myElement');
//clones the Element and append the clone after the Element.

replaceWith

Replaces the Element with an element passed.

Parameteres

ela 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.

Returns

the passed in element

Example

$('myOldElement').replaceWith($('myNewElement')); //$('myOldElement') is gone, and $('myNewElement') is in its place.

appendText

Appends text node to a DOM element.

Arguments

textthe text to append.

Example

<div id="myElement">hey</div>
$('myElement').appendText(' howdy'); //myElement innerHTML is now "hey howdy"

hasClass

Tests the Element to see if it has the passed in className.

Returns

truethe Element has the class
falseit doesn’t Arguments:
classNamethe class name to test.  Example:
<div id="myElement" class="testClass"></div>
$('myElement').hasClass('testClass'); //returns true

addClass

Adds the passed in class to the Element, if the element doesnt already have it.

Arguments

classNamethe class name to add

Example

<div id="myElement" class="testClass"></div>
$('myElement').addClass('newClass'); //<div id="myElement" class="testClass newClass"></div>

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.

Arguments

classNamethe class to add or remove

Example

<div id="myElement" class="myClass"></div>
$('myElement').toggleClass('myClass');
<div id="myElement" class=""></div>
$('myElement').toggleClass('myClass');
<div id="myElement" class="myClass"></div>

setStyle

Sets a css property to the Element.

Arguments

propertythe property to set
valuethe value to which to set it

Example

$('myElement').setStyle('width', '300px'); //the width is now 300px

setStyles

Applies a collection of styles to the Element.

Arguments

sourcean object or string containing all the styles to apply.  You cannot set the opacity using a string.

Examples

$('myElement').setStyles({
border: '1px solid #000',
width: '300px',
height: '400px'
});

OR

$('myElement').setStyles('border: 1px solid #000; width: 300px; height: 400px;');

setOpacity

Sets the opacity of the Element, and sets also visibility == “hidden” if opacity == 0, and visibility = “visible” if opacity > 0.

Arguments

opacityAccepts numbers from 0 to 1.

Example

$('myElement').setOpacity(0.5) //make it 50% transparent

getStyle

Returns the style of the Element given the property passed in.

Arguments

propertythe css style property you want to retrieve

Example

$('myElement').getStyle('width'); //returns "400px"
//but you can also use
$('myElement').getStyle('width').toInt(); //returns "400"

Returns

the style as a string

getStyles

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"}

addEvent

Attaches an event listener to a DOM element.

Arguments

typethe event to monitor (‘click’, ‘load’, etc) without the prefix ‘on’.
fnthe function to execute

Example

$('myElement').addEvent('click', function(){alert('clicked!')});

removeEvent

Works as Element.addEvent, but instead removes the previously added event listener.

removeEvents

removes all events of a certain type from an element. if no argument is passed in, removes all events.

fireEvent

executes all events of the specified type present in the element.

getPrevious

Returns the previousSibling of the Element, excluding text nodes.

Example

$('myElement').getPrevious(); //get the previous DOM element from myElement

Returns

the sibling element or undefined if none found.

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.  Returns as Elements.

setProperty

Sets an attribute for the Element.

Arguments

propertythe property to assign the value passed in
valuethe value to assign to the property passed in

Example

$('myImage').setProperty('src', 'whatever.gif'); //myImage now points to whatever.gif for its source

setProperties

Sets numerous attributes for the Element.

Arguments

sourcean object with key/value pairs.

Example

$('myElement').setProperties({
src: 'whatever.gif',
alt: 'whatever dude'
});
<img src="whatever.gif" alt="whatever dude">

setHTML

Sets the innerHTML of the Element.

Arguments

htmlthe new innerHTML for the element.

Example

$('myElement').setHTML(newHTML) //the innerHTML of myElement is now = newHTML

getProperty

Gets the an attribute of the Element.

Arguments

propertythe attribute to retrieve

Example

$('myImage').getProperty('src') // returns whatever.gif

Returns

the value, or an empty string

getTag

Returns the tagName of the element in lower case.

Example

$('myImage').getTag() // returns 'img'

Returns

The tag name in lower case

scrollTo

scrolls the element to the specified coordinated (if the element has an overflow)

Arguments

xthe x coordinate
ythe y coordinate

Example

$('myElement').scrollTo(0, 100)

getValue

Returns the value of the Element, if its tag is textarea, select or input. getValue called on a multiple select will return an array.

getSize

return an Object representing the size/scroll values of the element.

Example

$('myElement').getSize();

Returns

{
'scroll': {'x': 100, 'y': 100},
'size': {'x': 200, 'y': 400},
'scrollSize': {'x': 300, 'y': 500}
}

getPosition

Returns the real offsets of the element.

Example

$('element').getPosition();

Returns

{x: 100, y:500};

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

Example

var myValues = $('myElement').getCoordinates();

Returns

{
width: 200,
height: 300,
left: 100,
top: 50,
right: 300,
bottom: 350
}
function $(el)
returns the element passed in with all the Element prototypes applied.
function $$()
Selects, and extends DOM elements.
Inserts the Element before the passed element.
Adds the passed in class to the Element, if the element doesnt already have it.
Returns the previousSibling of the Element, excluding text nodes.
Custom class to allow all of its methods to be used with any DOM element via the dollar function $.
Css Query related function and Element extensions
Same as Element.injectBefore, but inserts the element inside.
Methods for dom queries arrays, <$$>.