JavaScript Kit > DOM Reference > Here
DOM Element methods
Below lists the DOM methods that can be used on most elements in a document:
DOM Element methods
| Properties | Description |
|---|---|
| addEventListener(eventType, listener, useCapture) | Associates a function with a particular event and
binds the event to the current node. NS/Firefox only.
addEventListener() accepts the following 3 parameters: 1)
EventType: A string representing the event to bind, without the
"on" prefix. For example, "click", "mousedown" etc. The advantage of using the DOM to bind an event is that you can assign multiple functions to a node for the same event (ie: window.onload) without running into event handler conflicts. Example(s): function statusreport(){ |
| attachEvent(eventType, function) | The IE5+ proprietary equivalent of addEventListener(). Note that
for the parameter eventType, the even string should include the "on"
prefix (ie: "onload", "onclick" etc). Example(s): if (window.attachEvent) |
| appendChild(node) | Inserts the specified node at the end of the current
node object. A frequently used method for dynamically appending a
new element or text to the document. Example(s): <div id="test"></div> |
| blur() | Removes keyboard focus from the current element. Used for example to fire the onBlur event handler of an element via scripting. |
| click() | Executes a click on a element. Used for example to fire the onClick event handler of an element via scripting. |
| cloneNode(deepBoolean) | Duplicates and returns a copy of the current node as
a standalone node (not part of document tree). Cloning a node copies both the
original's attributes and values, including the ID attribute, so be
sure to alter the cloned ID attribute's value so it's unique before
introducing it to the document tree. This method supports a single
Boolean parameter, "deepBoolean" that when set to true, clones all
the sub nodes of the current node as well, such as any text
contained within. Example(s): p=document.getElementById("mypara") |
| detachEvent(eventType, function) | Removes an event handler and its function previously
associated with a node in IE5+, via attachEvent() for example. The IE5+
proprietary equivalent of DOM2's removeEventListener(). Example(s): if (window.detachEvent) |
| dispatchEvent(eventObject) | Dispatches an event to fire on a node artificially.
This method returns a Boolean indicating whether any of the
listeners which handled the event called preventDefault (false if
called, otherwise, true). IE's equivalent of dispatchEvent() is
fireEvent(). Example(s): <div id="test" onclick="alert('hi')">Sample DIV.</div> |
| focus() | Sets focus on the current node. |
| getAttribute(attributeName) | Returns the value of the attribute named attribute
of the current node. Example(s): document.getElementById("test").getAttribute("align") |
| getAttributeNS(namespace, localname) | Returns the value of the attribute with the given local name and namespace. Applicable in XML documents. |
| getAttributeNode(attributename) | Returns/references the attribute of the current
element as a stand only node (not part of document tree). Example(s): var
attributeobj=document.getElementById("nav").getAttributeNode("align") |
| getAttributeNodeNS(namespace, localname) | Returns/references the attribute of the current element with the given local name and namespace. Applicable in XML documents. |
| getElementsByTagName(tagName) | Returns as an array all the child elements of the
current element matching the "tagName" parameter (ie: "li"). In
Firefox/ IE6+, you may enter an asterisk ("*") for the method's
parameter to retrieve a list of all elements within the current. Example(s): var mylist=document.getElementById("navlist") var alltags=document.getElementsByTagName("*") //returns all elements on page |
| getElementsByTagNameNS(namespace, localname) | Returns as an array all the child elements of the current element with the given local name and namespace. Applicable in XML documents. |
| hasAttribute(attributename) | Returns a Boolean value indicating whether the
current element contains an attribute (ie: "align"). Example(s): if (document.getElementById("mytable").hasAttribute("style")) |
| hasAttributeNS(namespace, localname) | Returns a Boolean value indicating whether the current element contains an attribute with the given local name and namespace. Applicable in XML documents. |
| hasAtrributes() | Returns a Boolean value indicating whether the current element has any explicit attributes defined. |
| hasChildNodes() | Returns a Boolean value indicating whether the current element contains any child nodes. |
| insertBefore(newElement, targetElement) | Updated. Inserts a new node "newElement" as a child node of
the current node. The "targetElement" property dictates where
"newElement" is inserted within the list of child nodes. If set to
null, the new element is inserted as the last child node; otherwise,
it's inserted right before "targetElement". Example(s): <div id="employees"> Important: Like many DOM methods that
change the structure of the document, insertBefore() can only be
called after the page has fully loaded. Doing so before will
return strange errors in most browsers! |
| insertAfter(newElement, targetElement) | Updated. As of DOM
Level 2 there does NOT exist an insertAfter() method. However, it
can be easily simulated using insertBefore() above. For the
"targetElement" parameter, just use "targetElement.nextSibling"
instead. <div id="employees">
|
| item(index) | Retrieves a node based on its index within the
document tree. IE4+ and FireFox1+. Example(s): <div id="div1"></div> |
| normalize() | Normalizes the current node and its sub tree. See here for more info. |
| removeAttribute(attributename) | Removes an attribute by its name. Example(s): document.getElementById("test").removeAttribute("href") |
| removeAttributeNode(attributereference) | Remove an attribute by passing in as parameter a
reference to the attribute object to remove. It offers an alternate
way to removeAttribrute()"for removing attributes, when all you
have is a reference to the attribute object in your script. Example(s): var
hrefattr=document.getElementById("test").getAttributeNode("href") |
| removeAttributeNS(namespace, localname) | Removes an attribute with the specified namespace and localname. |
| removeChild(childreference) | Removes the child node of the current node. The
removed node can then be reinserted elsewhere in the document tree. Example(s): <div id="father"><div id="child">A child</div></div> |
| removeEventListener(eventType, listener, useCapture) | Removes the specified event from being binded to the
current node: 1)
EventType: A string representing the event to unbind, without the
"on" prefix. For example, "click", "mousedown" etc. NS6/Firefox method. |
| replaceChild(newChild, oldChild) | Replaces one child node of the current node with
another child node. Example(s): <div id="adiv"><span
id="innerspan"></span></div> |
| scrollIntoView([Boolean]) | Firefox/IE4+ proprietary method that scrolls an element into view. It accepts an optional Boolean parameter that when set to true (default), scrolls the element so its top left corner touches the top of the viewable window. If false, the element's bottom left corner touches the bottom of the window. |
| setAttribute(attributename, value, [iecaseflag]) | Sets an attribute's value for the current element.
If the attribute doesn't exit yet, it creates the attribute first.
Otherwise, the existing attribute is modified with the new value. In
IE, the following two pitfalls exist:
Example(s): document.getElementById("test").setAttribute("title", "JavaScript Kit") |
| setAttributeNS(namespace, qualifiedname, value) | Sets or creates an attribute for the current node with the given local name and namespace. Applicable in XML documents. |
| setAttributeNode(attributereference) | Sets or creates an attribute for the current node.
"attributereference" should be a reference to a attribute you wish
to insert. If an attribute of the same name (as referenced) already
exists on the node, it is replaced with the newly inserted one. Example(s): <div id="brother" style="border:1px solid black; padding:
2px">Brother</div> |
| supports(feature, [version]) | Tests to see if this DOM implementation supports a particular feature. |
DD CSS Library
Free CSS menus and codes to give your site a visual boost.
