Back to the index.
DOM methods and properties that are for all implementations, and not just for the JavaScript one. In theory almost all of them should work in any programming language that supports the DOM.
This is the desktop table. See also the mobile table.
Last major update on 3 September 2013.
Creating elementscreateElement()
Create a new element Almost Yes Yes Yes Yes Yesvar x = document.createElement('P')Create a new HTML element node
<P>
and temporarily place it in x
, which is later inserted into the document.
('<P>')
.nodeName
for custom elements such as ppk
.createTextNode()
Create a new text node Almost Yes Yes Yes Yes Yesvar x = document.createTextNode('text')
Create a text node with content text
and temporarily place it in x
, which is later inserted into the document.
<style>
tag.Text() constructor
To create text nodes with a constructor. No No No No Yes Yesvar text = new Text('Oh, how quick that fox was!');
text
is now a text node that can be appended to the document.
These methods are meant for getting the HTML elements you need from the document.
You must know these methods by heart.
getElementById()
Get the element with this IDTest page Lower case 'd'!!
Almost Yes Yes Yes Yes Yesvar x = document.getElementById('test')
Take the element with id="test"
(wherever it is in the document) and put it in x
.
If there is more than one element with id="test"
, the method selects the first in the document. All others are ignored.
name="test"
.getElementsByClassName()
Get a nodeList of the elements with this class. No Yes Yes Yes Yes Yesdocument.getElementsByClassName('test') document.getElementsByClassName('test test2')
The first expression returns a nodeList with all elements that have a class
value that contains "test". The second one returns a nodeList will all elements that have a class
value that contains both "test" and "test2" (in any order).
getElementsByTagName()
Get all tags of this type Incomplete Yes Yes Yes Yes Yesvar x = document.getElementsByTagName('P')
Make x
into a nodeList of all P's in the document, so x[1]
is the second P etc.
var x = y.getElementsByTagName('P')
Gets all paragraphs that are descendants of node y
.
*
argument, which ought to select all elements in the document, doesn't work in IE 5.5.querySelector()
Get the first element that conforms to a CSS selector No Incomplete Yes Yes Yes Yes Yesdocument.querySelector('.testClass') document.querySelector('.testClass + p')
Returns the first element that have a class
value that contains "testClass"; or the first element that directly follows such an element.
div.className p
) are not supported.querySelectorAll()
Get a nodeList of elements by CSS selector No Incomplete Yes Yes Yes Yes Yesdocument.querySelectorAll('.testClass') document.querySelectorAll('.testClass + p')
Returns a nodeList with all elements that have a class
value that contains "testClass"; or a nodeList with all paragraphs directly following such an element.
Essentially, this method allows you to use CSS syntax to retrieve elements.
div.className p
) are not supported.These four properties give basic information about all nodes. What they return depends on the node type. They are read-only, except for nodeValue
.
There are three basic node types: element nodes (HTML tags), attribute nodes and text nodes. I test these properties for all these three types and added a fourth node type: the document node (the root of all other nodes).
You must know these properties by heart.
nodeName
The name of the node in UPPER CASE Incomplete Yes Yes Yes Yes Yesx.nodeName
The name of node x
. The correct names are:
#text
#comment
#document
nodeName
for attributes and the document.nodeName
of a comment as !
.nodeName
of an attribute testAttribute
? IE and Opera 12 retain the camelCase, the other browsers say testattribute
all lower-case. The spec is entirely silent on this subject.nodeType
The type of the node Incomplete Yes Yes Yes Yes Yesx.nodeType
The type of node x
. The correct types are:
nodeValue
The value of the node, if any. Read/write Incomplete Yes Yes Yes Yes Yesx.nodeValue
Get the value of node x
x.nodeValue = 'Test'
Set the value of node x
nodeValue
for attributestagName
The tag name of an element nodeTest page Don't use
Almost Yes Yes Yes Yes Yesx.tagName
Get the tag name of node x
. Correct values are:
My advice is not to use tagName
at all.nodeName
contains all functionalities of tagName
, plus a few more. Therefore nodeName
is always the better choice.
tagName
of a comment node is !
Five properties and two arrays for walking through the DOM tree. Using these properties, you can reach nodes that are close to the current node in the document structure.
In general you shouldn't use too many of these properties. As soon as you're doing something like
x.parentNode.firstChild.nextSibling.children[2]
your code is too complicated. Complex relationships between nodes can suddenly and unexpectedly change when you alter the document structure, and altering the document structure is the point of the W3C DOM. In general you should use only one or two of these properties per action.
You must know these properties by heart.
childNodes[]
An array with all child nodes of the node Incorrect Yes Yes Yes Yes Yesx.childNodes[1]
Get the second child node of node x
.
The childNodes
nodeList consists of all child nodes of the element, including (empty) text nodes and comment nodes.
childNodes
really contains the nodeList that children should give.
firstChild
The first child node of the node Incorrect Yes Yes Yes Yes Yesx.firstChild
Get the first child node of node x
.
hasChildNodes()
Check if the node has child nodes Yes Yes Yes Yes Yesx.hasChildNodes()
Returns true
when node x
has child nodes; false
when it hasn't.
lastChild
The last child node of the node Incorrect Yes Yes Yes Yes Yesx.lastChild
Get the last child of node x
.
nextSibling
The next sibling node of the node Incorrect Yes Yes Yes Yes Yesx.nextSibling
Get the next child of the parent of x
.
parentNode
The parent node of the node Yes Yes Yes Yes Yesx.parentNode
Get the parent node of x
.
previousSibling
The previous sibling node of the node Incorrect Yes Yes Yes Yes Yesx.previousSibling
Get the previous child of the parent of x
.
A few useful properties that should have been in the DOM from the start but mysteriously weren’t.
childElementCount
The number of element children No Yes Yes Yes Yes Yesx.childElementCount
children[]
An array with all child element nodes of the node Incorrect Yes Yes Yes Yes Yesx.children[1]
Get the second element child node of node x
.
Where childNodes
holds all child nodes, children
only holds those that are element nodes (HTML tags).
firstElementChild
The first child that is an element node No Yes Yes Yes Yes Yesx.firstElementChild
lastElementChild
The last child that is an element node No Yes Yes Yes Yes Yesx.lastElementChild
nextElementSibling
The next element node sibling No Yes Yes Yes Yes Yesx.nextElementSibling
previousElementSibling
The previous element node sibling No Yes Yes Yes Yes Yesx.previousElementSiblingNode manipulation
These five methods allow you to restructure the document. The average DOM script uses at least two of these methods.
The changes in the document structure are applied immediately, the whole DOM tree is altered. The browser, too, will immediately show the changes.
You must know these methods by heart.
appendChild()
Append a child node as the last node to an element Yes Yes Yes Yes Yesx.appendChild(y)
Make node y
the last child of node x
.
If you append a node that's somewhere else in the document, it moves to the new position.
cloneNode()
Clone a node Yes Yes Yes Yes Yesx = y.cloneNode(true | false)
Make node x
a copy of node y
. If the argument is true
, the entire tree below y
is copied, if it's false
only the root node y
is copied. Later you insert the clone into the document.
Note: Event handlers are not cloned. This is an error in the spec.
Also, eventually cloneNode()
without argument will mean cloneNode(true)
, as it should have from the start. Only Firefox supports this yet, as did Presto-based Opera 12 (but not Blink-based Opera 15/16).
insertBefore()
Insert a node into the child nodes of an element Yes Yes Yes Yes Yesx.insertBefore(y,z)
Insert node y
as a child of node x
just before node z
.
removeChild()
Remove a child node from an element Yes Yes Yes Yes Yesx.removeChild(y)
Remove child y
of node x
.
replaceChild()
Replace a child node of an element by another child node Yes Yes Yes Yes Yesx.replaceChild(y,z)
Replace node z
, a child of node x
, by node y
.
These methods are brand-new. They should have been in the DOM from the start.
after()
Add a node after another node No No No No Nox.after(y)
Insert node y
just after node x
.
append()
Add a node as the last child No No No No Nox.append(y)
Insert node y
as the last child of node x
.
(This is exactly the same as appendChild()
)
before()
Add a node before another node No No No No Nox.before(y)
Insert node y
just before node x
.
prepend()
Add a node as the first child No No No No Nox.prepend(y)
Insert node y
as the first child of node x
.
remove()
Remove a node No Yes No No Yes Yesx.remove()
Remove node x
from the document.
No more x.parentNode.removeChild(x)
, in other words.
replace()
Replace a node by another node No No No No Nox.replace(y)
Replace node x
by node y
.
These methods are for manipulating text data, i.e. the contents of text nodes.
appendData()
Append data to a text node No Yes Yes Yes Yes Yesx.appendData(' some extra text')
Appends the string some extra text
to x, which must be a text node.
data
The content of a text node Yes Yes Yes Yes Yesx.data
The content of x
, which must be a text node. The same as x.nodeValue
.
Can also be set:
x.data = 'The new text'
deleteData()
Delete text from a text node No Yes Yes Yes Yes Yesx.deleteData(4,3)
Delete some data from x
, which must be a text node, starting at the fifth character and deleting three characters. Second argument is required.
insertData()
Insert text into a text node No Yes Yes Yes Yes Yesx.insertData(4,' and now for some extra text ')
Insert the string and now for some extra text
after the fourth character into x
, which must be a text node.
normalize()
Merge adjacent text nodes into one node No Yes Yes Yes Yes Yesx.normalize()
All child nodes of node x
that are text nodes and have other text nodes as siblings, are merged. This is in fact the reverse of splitText
: text nodes that were split, come together again.
replaceData()
Replace text in a text node No Yes Yes Yes Yes Yesx.replaceData(4,3,' and for some new text ')
Replace three characters, beginning at the fifth one, of node x
, which must be a text node, by the string and for some new text
.
splitText()
Split a text node into two text nodes Buggy Yes Yes Yes Yes Yesx.splitText(5)
Split the text node x
at the 6th character. x
now contains the first part (char. 0-5), while a new node is created (and becomes x.nextSibling
) which contains the second part (char. 6-end) of the orginial text.
splitText()
fine, but after you’ve normalized the text IE doesn’t split it any more.substringData()
Take a substring of the text in the text node No Yes Yes Yes Yes Yesx.substringData(4,3)
Takes a substring of x
, which must be a text node, starting at the fifth character and with a length of three characters. Thus it's the same as the old substr() method of strings.
wholeText
The text of a text node plus the text in directly adjacent text nodes. Read only. No Yes Yes Yes Yes YesThis read-only property is useful if you want to get the entire text at a certain point and don’t want to be bothered by borders between text nodes.
AttributesA bloody mess. Try influencing attributes in this order:
x.id
or y.onclick
.getAttribute()
or setAttribute()
.attributes[]
. It's worse than anything else.In my view any method or property concerning attribute nodes should also work on the style
attribute, event handlers and custom attributes. If not I judge the method or property incomplete.
attributes[index]
An array with the attributes of a node, accessed by index number, in the order they're defined in the source code.Test page Do not use Use getAttribute()
instead
x.attributes[1]
This array consists of all defined attributes in the source code order .
Do yourself a favour and don't use the indexed attributes
array.
attributes[key]
An array with the attributes of a node, accessed by attribute name Incorrect Incomplete Almost Yes Yes Yes Yesx.attributes['align']
Get the align attribute object of node x
. If the node has no align attribute, it returns undefined
(except in IE, where it returns an attribute object that has no value.)
style
attribute.color: green
, IE8 returns COLOR: green
, and IE9 and up color: green;
(with semicolon).createAttribute()
Create a new attribute node No Yes Yes Yes Yes Yesz = document.createAttribute('title'); z.value = 'Test title'; x.setAttributeNode(z)
This creates a title attribute with a value and sets it on node x
.
getAttribute()
Get the value of an attribute Incomplete Almost Yes Yes Yes Yesx.getAttribute('align')
Gives the value of the align attribute of node x
. Upper case attribute names are also allowed.
color: green
, IE8 returns COLOR: green
, and IE9 and up color: green;
(with semicolon).getAttributeNode()
Get an attribute node No Incomplete Almost Yes Yes Yes Yesx.getAttributeNode('align')
Get the attribute object align
of node x
. This is an object, not a value.
x.getAttributeNode('style')
.color: green
, IE8 returns COLOR: green
, and IE9 and up color: green;
(with semicolon).hasAttribute()
Check if a node has a certain attribute No Yes Yes Yes Yes Yesx.hasAttribute('align')
Returns true
when node x
has an align attribute, false
when it hasn't.
hasAttributes()
Check if a node has attributes No Yes Yes Yes Yes Yesx.hasAttributes()
Returns true
when node x
has attributes, false
when it hasn't.
name
The name of an attribute No Yes Yes Yes Almost Yes Yesx.name
The name of attribute node x
.
removeAttribute()
Remove an attribute node Almost Yes Weird Yes Almost Yes Yes Yes Yesx.removeAttribute('align')
Remove the align
attribute from node x
.
removeAttribute
to remove event handlers.removeAttributeNode()
Remove an attribute node No Minimal Almost Yes Almost Yes Yes Yes Yesx.removeAttributeNode(x.attributes['align']) x.removeAttributeNode(x.attributes[1]) x.removeAttributeNode(x.getAttributeNode('align'))
Removes the attribute node. There is little difference with removeAttribute()
, except in the argument.
removeAttribute()
for event handlers in IE. Exception: they are not removed in IE10, either.setAttribute()
Set the value of an attribute Incomplete Yes Yes Yes Yes Yesx.setAttribute('align','right')
Set the align attribute of node x
to right
. The name and value are both strings.
setAttributeNode()
No Incomplete Yes Yes Yes Yes Yesx.setAttributeNode(node)
Add attribute node node
to the element.
value
The value of an attribute No Incomplete Yes Yes Yes Yes Yesx.value
The value of attribute x
.
null
for style values.color: green
, IE8 returns COLOR: green
, and IE9 and up color: green;
(with semicolon).A lot of miscellaneous methods and properties that you'll rarely need. I use only two of them in an actual script.
compareDocumentPosition()
Gives the relative place of one element compared to another. No Yes Yes Yes Yes Yesx.compareDocumentPosition(y)
Compares the document (DOM) position of element y
to that of element x
The method returns a bitmask:
All relevant numbers are added, and this sum is returned. So if y
follows (4) and is contained by (16) x
, the method returns 20.
contains()
Check whether an element contains another element Yes Yes Yes Yes Yesx.contains(y)
If node y
is a descendant of node x
, the method returns true
, else false
.
createDocumentFragment()
Create a document fragment No Yes Yes Yes Yes Yesx = document.createDocumentFragment(); x.[fill with nodes]; document.[somewhere].appendChild(x);
Create a fragment, add a lot of nodes to it, and then insert it into the document. Note that the fragment itself is not inserted, only its child nodes.
documentElement
The HTML tag Yes Yes Yes Yes Yesdocument.documentElement
Represents the root element of the XML document. In any HTML document, the <html>
element is of course the root element.
getElementsByName()
Get elements by their name attribute Incorrect and incomplete Yes Yes Yes Yes Yesvar x = document.getElementsByName('test')
Create a nodeList with all elements that have name="test"
. It should ignore elements with id="test"
On my test page the <p>
, <input>
, <img>
and <ppk>
tags have this name, while there's also a paragraph with id="test"
. Ideally, all browsers should get the first four elements and ignore the fifth one.
<p>
and <ppk>
tags with name="test"
, but counts the <div>
with id="test"
isEqualNode()
Whether two nodes are the same No Yes Yes Yes Yes Yesx.isEqualNode(y)
Returns true
when x
and y
refer to the same node; false
if they don’t.
ownerDocument
The document that 'owns' the element No Yes Yes Yes Yes Yesx.ownerDocument
Refers to the document object that 'owns' node x
. This is the document node.
As usual Microsoft has extended the standard somewhat. Though sometimes its extensions are brilliant (innerHTML
springs to mind), in the case of the DOM Core they aren't.
Note the difference between W3C and Microsoft methods. The W3C methods are owned by the parent element of the node you want to adjust, the Microsoft methods by the node itself.
applyElement()
Something with nodes Yes No No No Novar y = document.createElement('i'); x.applyElement(y)
The <i>
element is inserted into element x
, around the text.
clearAttributes()
Remove all attributes from a node Incomplete No No No Nox.clearAttributes()
Remove all attributes from node x
.
mergeAttributes()
Copy all attributes of one node to another node Yes No No No Nox.mergeAttributes(y)
Copy all of node y
's attributes to node x
.
removeNode()
Remove a node Yes No No No Nox.removeNode(true | false)
Remove node x
from the document. If you use the argument true
its children are also removed; if you use false
they aren't. Note that all text nodes count as children, too.
replaceNode()
Replace a node by another node Yes No No No Nox.replaceNode(y)
Replace node x
by node y
.
sourceIndex
The index number of the node in the page source Yes Incorrect No No No Nox.sourceIndex
Get the sourceIndex
of element x
. This is also the index number for the element in the document.getElementsByTagName('*')
array.
sourceIndex
that’s one higher than it should be, and thus the link to document.getElementsByTagName('*')
is lost. This extra element is the DOCTYPE declaration.swapNode()
Swap two nodes Yes No No No Nox.swapNode(y)
Put node x
in node y
's place and vice versa.
Desktop browser test array 1.0; September 2013
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4