class Element
Related utilities
Methods
absolutizeaddClassNameaddMethodsadjacentancestorschildElementsclassNamescleanWhitespacecloneclonePositioncumulativeOffsetcumulativeScrollOffsetdescendantOfdescendantsdownemptyextendfirefirstDescendantgetDimensionsgetElementsBySelectorgetHeightgetOffsetParentgetOpacitygetStoragegetStylegetWidthhasClassNamehideidentifyimmediateDescendantsinsertinspectmakeClippingmakePositionedmatchnewnextnextSiblingsobservepositionedOffsetpreviouspreviousSiblingsreadAttributerecursivelyCollectrelativizeremoveremoveClassNamereplaceretrievescrollToselectsetOpacitysetStyleshowsiblingsstopObservingstoretoggletoggleClassNameundoClippingundoPositionedupupdateviewportOffsetvisiblewrapwriteAttribute
Constructor
Class methods
-
absolutize #
Element.absolutize(element) ⇒ Element
Element#absolutize() ⇒ ElementTurns
elementinto an absolutely-positioned element without changing its position in the page layout.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
addClassName #
Element.addClassName(element, className) ⇒ Element
Element#addClassName(className) ⇒ ElementAdds a CSS class to
element.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
addMethods #
Element.addMethods(methods) ⇒ undefined
Element.addMethods(tagName, methods) ⇒ undefinedTakes a hash of methods and makes them available as methods of extended elements and of the
Elementobject.The second usage form is for adding methods only to specific tag names.
-
adjacent #
Element.adjacent(element, selector...) ⇒ [Element…]
Element#adjacent(selector...) ⇒ [Element…]-
selector(String) – A CSS selector.
Finds all siblings of the current element that match the given selector(s).
This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
-
ancestors #
Element.ancestors(element) ⇒ [Element…]
Element#ancestors() ⇒ [Element…]Collects all of
element's ancestors and returns them as an array of elements.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
childElements #
Element.childElements(element) ⇒ [Element…]
Element#childElements() ⇒ [Element…]Alias of:
Element.immediateDescendantsThis method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
classNames #
Element.classNames(element) ⇒ [String…]
Element#classNames() ⇒ [String…]Returns a new instance of Element.ClassNames, an
Enumerableobject used to read and write CSS class names ofelement.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
cleanWhitespace #
Element.cleanWhitespace(element) ⇒ Element
Element#cleanWhitespace() ⇒ ElementRemoves whitespace-only text node children from
element.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
clone #
Element.clone(element, deep) ⇒ Element
Element#clone(deep) ⇒ Element-
deep(Boolean) – Whether to cloneelement's descendants as well.
Returns a duplicate of
element.A wrapper around DOM Level 2
Node#cloneNode,Element#clonecleans up any expando properties defined by Prototype.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
-
clonePosition #
Element.clonePosition(element, source[, options]) ⇒ Element
Element#clonePosition(source[, options]) ⇒ ElementClones the position and/or dimensions of
sourceontoelementas defined byoptions.Valid keys for
optionsare:setLeft,setTop,setWidth, andsetHeight(all booleans which default totrue); andoffsetTopandoffsetLeft(numbers which default to0). Use these to control which aspects ofsource's layout are cloned and how much to offset the resulting position ofelement.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
cumulativeOffset #
Element.cumulativeOffset(element) ⇒ Array
Element#cumulativeOffset() ⇒ ArrayReturns the offsets of
elementfrom the top left corner of the document.Returns an array in the form of
[leftValue, topValue]. Also accessible as properties:{ left: leftValue, top: topValue }.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
cumulativeScrollOffset #
Element.cumulativeScrollOffset(element) ⇒ Array
Element#cumulativeScrollOffset() ⇒ ArrayCalculates the cumulative scroll offset of an element in nested scrolling containers.
Returns an array in the form of
[leftValue, topValue]. Also accessible as properties:{ left: leftValue, top: topValue }.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
descendantOf #
Element.descendantOf(element, ancestor) ⇒ Boolean
Element#descendantOf(ancestor) ⇒ BooleanChecks if
elementis a descendant ofancestor.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
descendants #
Element.descendants(element) ⇒ [Element…]
Element#descendants() ⇒ [Element…]Collects all of element's descendants and returns them as an array of elements.
This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
down #
Element.down(element[, expression[, index = 0]]) ⇒ Element
Element#down([expression[, index = 0]]) ⇒ Element
Element#down([index = 0]) ⇒ Element-
expression(String) – A CSS selector.
Returns
element's first descendant (or the Nth descendant, ifindexis specified) that matchesexpression. If noexpressionis provided, all descendants are considered. If no descendant matches these criteria,undefinedis returned.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
-
empty #
Element.empty(element) ⇒ Element
Element#empty() ⇒ ElementTests whether
elementis empty (i.e., contains only whitespace).This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
extend #
Element.extend(element) ⇒ ElementExtends
elementwith all of the methods contained inElement.MethodsandElement.Methods.Simulated. Ifelementis aninput,textarea, orselecttag, it will also be extended with the methods fromForm.Element.Methods. If it is aformtag, it will also be extended with the methods fromForm.Methods. -
fire #
Element.fire(element, eventName[, memo[, bubble = true]]) ⇒ Event
Element#fire(eventName[, memo[, bubble = true]]) ⇒ EventSee
Event.fire.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
firstDescendant #
Element.firstDescendant(element) ⇒ Element
Element#firstDescendant() ⇒ ElementReturns the first child that is an element.
This is opposed to the
firstChildDOM property, which will return any node, including text nodes.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
getDimensions #
Element.getDimensions(element) ⇒ Object
Element#getDimensions() ⇒ ObjectFinds the computed width and height of
elementand returns them as key/value pairs of an object.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
getElementsBySelector #
Element.getElementsBySelector(element, selector) ⇒ [Element…]
Element#getElementsBySelector(selector) ⇒ [Element…]Alias of:
Element.selectThis method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
getHeight #
Element.getHeight(element) ⇒ Number
Element#getHeight() ⇒ NumberReturns the height of
element.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
getOffsetParent #
Element.getOffsetParent(element) ⇒ Element
Element#getOffsetParent() ⇒ ElementReturns
element's closest positioned ancestor. If none is found, thebodyelement is returned.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
getOpacity #
Element.getOpacity(element) ⇒ String | null
Element#getOpacity() ⇒ String | nullReturns the opacity of the element.
This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
getStorage #
Element.getStorage(element) ⇒ Hash
Element#getStorage() ⇒ HashReturns the
Hashobject that stores custom metadata for this element.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
getStyle #
Element.getStyle(element, style) ⇒ String | null
Element#getStyle(style) ⇒ String | null-
style(String) – The property name to be retrieved.
Returns the given CSS property value of
element. The property can be specified in either its CSS form (font-size) or its camelized form (fontSize).This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
-
getWidth #
Element.getWidth(element) ⇒ Number
Element#getWidth() ⇒ NumberReturns the width of
element.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
hasClassName #
Element.hasClassName(element, className) ⇒ Boolean
Element#hasClassName(className) ⇒ BooleanChecks whether
elementhas the given CSS class name.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
hide #
Element.hide(element) ⇒ Element
Element#hide() ⇒ ElementSets
display: noneonelement. Returnselement.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
identify #
Element.identify(element) ⇒ String
Element#identify() ⇒ StringReturns
element's ID. Ifelementdoes not have an ID, one is generated, assigned toelement, and returned.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
immediateDescendants #
Element.immediateDescendants(element) ⇒ [Element…]
Element#immediateDescendants() ⇒ [Element…]Collects all of
element's immediate descendants (i.e., children) and returns them as an array of elements.Aliased as:
Element.childElementsThis method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
insert #
Element.insert(element, content) ⇒ Element
Element#insert(content) ⇒ ElementInserts content
above,below, at thetop, and/or at thebottomof the given element, depending on the option(s) given.insertaccepts content in any of these forms:String: A string of HTML to be parsed and renderedElement: An Element instance to insert- ...any object with a
toElementmethod: The method is called and the resulting element used - ...any object with a
toHTMLmethod: The method is called and the resulting HTML string is parsed and rendered
The
contentargument can be the content to insert, in which case the implied insertion point isbottom, or an object that specifies one or more insertion points (e.g.,{ bottom: "foo", top: "bar" }).Accepted insertion points are:
before(aselement's previous sibling)after(aselement'snext sibling)top(aselement's first child)bottom(aselement's last child)
Examples
Insert the given HTML at the bottom of the element (using the default):
$('myelement').insert("<p>HTML to append</p>"); $('myelement').insert({ top: new Element('img', {src: 'logo.png'}) });Put
hrsbeforeandafterthe element:$('myelement').insert({ before: "<hr>", after: "<hr>" });This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
inspect #
Element.inspect(element) ⇒ String
Element#inspect() ⇒ StringReturns the debug-oriented string representation of
element.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
makeClipping #
Element.makeClipping(element) ⇒ Element
Element#makeClipping() ⇒ ElementSimulates the poorly-supported CSS
clipproperty by settingelement'soverflowvalue tohidden.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
makePositioned #
Element.makePositioned(element) ⇒ Element
Element#makePositioned() ⇒ ElementAllows for the easy creation of a CSS containing block by setting
element's CSSpositiontorelativeif its initial position is eitherstaticorundefined.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
match #
Element.match(element, selector) ⇒ boolean
Element#match(selector) ⇒ boolean-
selector(String) – A CSS selector.
Checks if
elementmatches the given CSS selector.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
-
next #
Element.next(element[, expression[, index = 0]]) ⇒ Element
Element#next([expression[, index = 0]]) ⇒ Element
Element#next([index = 0]) ⇒ Element-
expression(String) – A CSS selector.
Returns
element's first following sibling (or the Nth, ifindexis specified) that matchesexpression. If noexpressionis provided, all following siblings are considered. If none matches these criteria,undefinedis returned.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
-
nextSiblings #
Element.nextSiblings(element) ⇒ [Element…]
Element#nextSiblings() ⇒ [Element…]Collects all of
element's next siblings and returns them as an array of elements.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
observe #
Element.observe(element, eventName, handler) ⇒ Element
Element#observe(eventName, handler) ⇒ ElementSee
Event.observe.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
positionedOffset #
Element.positionedOffset(element) ⇒ Array
Element#positionedOffset() ⇒ ArrayReturns
element's offset relative to its closest positioned ancestor (the element that would be returned byElement.getOffsetParent).Returns an array in the form of
[leftValue, topValue]. Also accessible as properties:{ left: leftValue, top: topValue }.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
previous #
Element.previous(element[, expression[, index = 0]]) ⇒ Element
Element#previous([expression[, index = 0]]) ⇒ Element
Element#previous([index = 0]) ⇒ Element-
expression(String) – A CSS selector.
Returns
element's first previous sibling (or the Nth, ifindexis specified) that matchesexpression. If noexpressionis provided, all previous siblings are considered. If none matches these criteria,undefinedis returned.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
-
previousSiblings #
Element.previousSiblings(element) ⇒ [Element…]
Element#previousSiblings() ⇒ [Element…]Collects all of
element's previous siblings and returns them as an array of elements.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
readAttribute #
Element.readAttribute(element, attributeName) ⇒ String | null
Element#readAttribute(attributeName) ⇒ String | nullReturns the value of
element's attribute with the given name.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
recursivelyCollect #
Element.recursivelyCollect(element, property) ⇒ [Element…]
Element#recursivelyCollect(property) ⇒ [Element…]Recursively collects elements whose relationship to
elementis specified byproperty.propertyhas to be a property (a method won't do!) ofelementthat points to a single DOM node (e.g.,nextSiblingorparentNode).This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
relativize #
Element.relativize(element) ⇒ Element
Element#relativize() ⇒ ElementTurns
elementinto a relatively-positioned element without changing its position in the page layout.Used to undo a call to
Element.absolutize.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
remove #
Element.remove(element) ⇒ Element
Element#remove() ⇒ ElementCompletely removes
elementfrom the document and returns it.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
removeClassName #
Element.removeClassName(element, className) ⇒ Element
Element#removeClassName(className) ⇒ ElementRemoves a CSS class from
element.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
replace #
Element.replace(element[, newContent]) ⇒ Element
Element#replace([newContent]) ⇒ ElementReplaces
elementitself withnewContentand returnselement.Keep in mind that this method returns the element that has just been removed — not the element that took its place.
This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
retrieve #
Element.retrieve(element, key[, defaultValue]) ⇒ ?
Element#retrieve(key[, defaultValue]) ⇒ ?Retrieves custom metadata set on
elementwithElement.store.If the value is
undefinedanddefaultValueis given, it will be stored on the element as its new value for that key, then returned.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
scrollTo #
Element.scrollTo(element) ⇒ Element
Element#scrollTo() ⇒ ElementScrolls the window so that
elementappears at the top of the viewport.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
select #
Element.select(element, selector...) ⇒ [Element…]
Element#select(selector...) ⇒ [Element…]-
selector(String) – A CSS selector.
Takes an arbitrary number of CSS selectors and returns an array of descendants of
elementthat match any of them.Aliased as:
Element.getElementsBySelectorThis method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
-
setOpacity #
Element.setOpacity(element, value) ⇒ Element
Element#setOpacity(value) ⇒ ElementSets the opacity of
element.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
setStyle #
Element.setStyle(element, styles) ⇒ Element
Element#setStyle(styles) ⇒ ElementModifies
element's CSS style properties.Styles are passed as an object of property-value pairs in which the properties are specified in their camelized form (e.g.,
fontSize).This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
show #
Element.show(element) ⇒ Element
Element#show() ⇒ ElementRemoves
display: noneonelement. Returnselement.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
siblings #
Element.siblings(element) ⇒ [Element…]
Element#siblings() ⇒ [Element…]Collects all of element's siblings and returns them as an array of elements.
This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
stopObserving #
Element.stopObserving(element[, eventName[, handler]]) ⇒ Element
Element#stopObserving([eventName[, handler]]) ⇒ ElementSee
Event.stopObserving.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
store #
Element.store(element, key, value) ⇒ Element
Element#store(key, value) ⇒ ElementStores a key/value pair of custom metadata on the element.
The metadata can later be retrieved with
Element.retrieve.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
toggle #
Element.toggle(element) ⇒ Element
Element#toggle() ⇒ ElementToggles the visibility of
element. Returnselement.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
toggleClassName #
Element.toggleClassName(element, className) ⇒ Element
Element#toggleClassName(className) ⇒ ElementToggles the presence of a CSS class on
element.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
undoClipping #
Element.undoClipping(element) ⇒ Element
Element#undoClipping() ⇒ ElementSets
element's CSSoverflowproperty back to the value it had beforeElement.makeClippingwas applied.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
undoPositioned #
Element.undoPositioned(element) ⇒ Element
Element#undoPositioned() ⇒ ElementSets
elementback to the state it was in beforeElement.makePositionedwas applied to it.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
up #
Element.up(element[, expression[, index = 0]]) ⇒ Element
Element#up([expression[, index = 0]]) ⇒ Element
Element#up([index = 0]) ⇒ Element-
expression(String) – A CSS selector.
Returns
element's first ancestor (or the Nth ancestor, ifindexis specified) that matchesexpression. If noexpressionis provided, all ancestors are considered. If no ancestor matches these criteria,undefinedis returned.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
-
update #
Element.update(element[, newContent]) ⇒ Element
Element#update([newContent]) ⇒ ElementReplaces the content of
elementwith thenewContentargument and returnselement.newContentmay be in any of these forms:String: A string of HTML to be parsed and renderedElement: An Element instance to insert- ...any object with a
toElementmethod: The method is called and the resulting element used - ...any object with a
toHTMLmethod: The method is called and the resulting HTML string is parsed and rendered
If
newContentis omitted, the element's content is blanked out (i.e., replaced with an empty string).If
newContentis a string and contains one or more inline<script>tags, the scripts are scheduled to be evaluated after a very brief pause (usingFunction#defer) to allow the browser to finish updating the DOM. Note that the scripts are evaluated in the scope ofString#evalScripts, not in the global scope, which has important ramifications for yourvars andfunctions. SeeString#evalScriptsfor details.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
viewportOffset #
Element.viewportOffset(element) ⇒ Array
Element#viewportOffset() ⇒ ArrayReturns the X/Y coordinates of element relative to the viewport.
Returns an array in the form of
[leftValue, topValue]. Also accessible as properties:{ left: leftValue, top: topValue }.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
visible #
Element.visible(element) ⇒ boolean
Element#visible() ⇒ booleanTells whether
elementis visible (i.e., whether its inlinedisplayCSS property is set tonone.This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
wrap #
Element.wrap(element, wrapper[, attributes]) ⇒ Element
Element#wrap(wrapper[, attributes]) ⇒ Element-
wrapper(Element|String) – An element to wrapelementinside, or else a string representing the tag name of an element to be created. -
attributes(Object) – A set of attributes to apply to the wrapper element. Refer to theElementconstructor for usage.
Wraps an element inside another, then returns the wrapper.
This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.
-
-
writeAttribute #
Element.writeAttribute(element, attribute[, value = true]) ⇒ Element
Element#writeAttribute(attribute[, value = true]) ⇒ Element
Element#writeAttribute(attributes) ⇒ ElementAdds, changes, or removes attributes passed as either a hash or a name/value pair.
This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.