HTML elements, comments, text, the doctype and the document are instances of this class. It provides methods to work on the DOM tree and manipulate elements.
childNodes
ArrayArray that contains every child node of the element
parentNode
ObjectReference to the parent node of the element or null if there isn't
previousSibling
ObjectReference to the previous sibling of the element or null if there isn't
nextSibling
ObjectReference to the next sibling of the element or null if there isn't
firstChild
ObjectReference to the first child node of the element or null if there isn't
lastChild
ObjectReference to the last child node of the element or null if there isn't
ownerDocument
ObjectReference to the document object
attributes
ObjectObject that contains element's attributes
style
ObjectObject that contains element's style properties
index
IntElement's index in its parent node childNodes array
nodeType
IntElement's node type. It's value is one of the node type constants
nodeName
StringElement's node name
nodeValue
StringElement's node value
tagName
StringElement's tag name. Null for document, doctype, comments and text nodes
textContent
StringIt contains the text of text and comments nodes and the code inside script and style tags
name
StringThis property is owned only by the doctype element and it's the doctype name
publicId
StringThis property is owned only by the doctype element and it's the doctype public id
systemId
StringThis property is owned only by the doctype element and it's the doctype system id
setAttributes
Set multiple attributes
Arguments
mixed $attributes
Array or object of attributes in attribute=>value format or html attribute string
ReturnObject Reference to the current element
Example
$element=& $document->body->childNodes[2];
$element->setAttributes(array("class"=>"myclass","id"=>"myid")); //array
$element->setAttributes("class='myclass' id='myid'"); //html attribute string
setAttribute
Set an attribute
Arguments
string $name
Attribute name
string $val
Attribute value
ReturnObject Reference to the current element
Example
$element=& $document->body->firstChild;
$element->setAttribute("name","myname");
hasAttribute
Check if the element has a given attribute
Arguments
string $name
Attribute name
ReturnBool True if the element has that attribute, otherwise false
Example
if($element->parentNode->hasAttribute("id"))
{
...
}
getAttribute
Return the value of the given attribute or null if the element hasn't that attribute
Arguments
string $name
Attribute name
ReturnMixed The attribute value or null if the element hasn't that attribute
Example
$id=$element->getAttribute("id");
removeAttribute
Remove the given attribute from the element
Arguments
string $name
Attribute name
ReturnObject Reference to the current element
Example
$element->setAttribute("id","myid");
$element->removeAttribute("id");
$id=$element->getAttribute("id"); //null because it was removed
hasAttributes
Check if the element has at least one attribute
ReturnBool True if the element has at least one attribute, otherwise false
Example
$element=&$document->createElement("span");
$check=$element->hasAttributes(); //False
$element->setAttribute("id","myid");
$check=$element->hasAttributes(); //True
emptyAttributes
Remove all attributes from the element
ReturnObject Reference to the current element
Example
$check=$element->hasAttributes(); //True
$element->emptyAttributes();
$check=$element->hasAttributes(); //False
setStyles
Set multiple style properties
Arguments
mixed $styles
Array or object of style properties in style=>value format or css style string
ReturnObject Reference to the current element
Example
$element=& $document->body->childNodes[2];
$element->setStyles(array("padding"=>"5px","z-index"=>"2")); //array
$element->setStyles("padding:5px; z-index:2"); //css style string
setStyle
Set a style property
Arguments
string $name
Style property name
string $val
Style property value
ReturnObject Reference to the current element
Example
$element=& $document->body->firstChild;
$element->setStyle("margin","1px");
hasStyle
Check if the element has a given style property
Arguments
string $name
Style property name
ReturnBool True if the element has that style property, otherwise false
Example
if($element->parentNode->hasStyle("background"))
{
...
}
getStyle
Return the value of the given style property or null if the element hasn't that style property
Arguments
string $name
Style property name
ReturnMixed The style property value or null if the element hasn't that style property
Example
$color=$element->getStyle("color");
removeStyle
Remove the given style property from the element
Arguments
string $name
Style property name
ReturnObject Reference to the current element
Example
$element->setStyle("font-size","12px");
$element->removeStyle("font-size");
$size=$element->getStyle("font-size"); //null because it was removed
hasStyles
Check if the element has at least one style property
ReturnBool True if the element has at least one style property, otherwise false
Example
$element=&$document->createElement("span");
$check=$element->hasStyles(); //False
$element->setStyle("text-decoration","underline");
$check=$element->hasStyles(); //True
emptyStyles
Remove all style properties from the element
ReturnObject Reference to the current element
Example
$check=$element->hasStyles(); //True
$element->emptyStyles();
$check=$element->hasStyles(); //False
setCssText
Same as setStyles, but it empties the style object before setting new style properties
Arguments
string $style
Style properties in css formatted string
ReturnObject Reference to the current element
Example
$element->setCssText("background:red;margin:0");
getCssText
Get the style object in css formatted string
ReturnString Css formatted string of the style object
Example
$element=&$document->createElement("table");
$element->setCssText("background:red; margin:0");
echo $element->getCssText(); //returns "background:red;margin:0"
setInnerHTML
Set element's inner code by parsing the given HTML string
Arguments
string $HTML
HTML code
ReturnObject Reference to the current element
Example
$element=& $document->head->firstChild->nextSibling;
echo count($element->childNodes); //0: this element hasn't got any child node
$element->setInnerHTML("<title>Example</title>");
echo count($element->childNodes); //1: Now the element has a child node
echo $element->childNodes[0]->tagName; //1: The child node is a title element
getInnerHTML
Get the HTML code inside the element.
ReturnString HTML code inside the element
Example
$element->setInnerHTML("<title>Example</title>");
echo $element->getInnerHTML(); //Prints "<title>Example</title>"
getOuterHTML
Get the HTML code of the element and its innerHTML.
ReturnString HTML element code and the code inside it
Example
$element=& $document->createElement("span");
$element->setInnerHTML("<div>Example</div>");
echo $element->getOuterHTML(); //Prints "<span><div>Example</div></span>"
getTextContent
Return the text content of the element and its children
ReturnString Text content
Example
$element=& $document->createElement("span");
$element->setInnerHTML("This is an <div>Example</div>");
echo $element->getTextContent(); //Prints "This is an Example"
getOuterTextContent
Return the getTextContent function applied on the current element and its sibling nodes
ReturnString Text content
Example
$element=& $document->createElement("span");
$element->setInnerHTML("<span>This is an </span><div>Example</div>");
echo $element->childNodes[0]->getOuterTextContent(); //Prints "This is an Example"
getChildAt
Get the child node at the specified position or null if there's no element at that index
Arguments
int $index
Child node index
ReturnObject HTML element or null if there's no element at that index
Example
$child=& $element->getChildAt(2);
$child=& $element->->childNodes[2]; //Same operation
getElementsByTagName
Get elements inside the current node with the given tag name. It returns an HTMLCollection object.
Arguments
string $tag
Tag name
ReturnObject HTML collection
Example
$allDivs=& $document->body->getElementsByTagName("div"); //Find all div elements inside the body
getElementsByClassName
Get elements inside the current node with the given class name. It returns an HTMLCollection object.
Arguments
string $class
Class name
ReturnObject HTML collection
Example
$class=& $document->body->getElementsByClassName("myclass"); //Find all elements with the given class attribute
getElementById
Get the element with the given id inside the current node or null if there's no element with that id
Arguments
string $id
Id
ReturnObject HTML element or null
Example
$myid=& $document->body->getElementsById("myid"); //Find the element with "myid" id
getElementsBySelector
Get every element inside the current element that matches the given css selector. For a list of all supported selectors see the HTMLCollection section.
Arguments
string $selector
Css selector
ReturnObject HTML collection
Example
$col=& $document->getElementsBySelector("div.myclass"); //Find all divs with "myclass" class name
getElementsBySelector
Get every element inside the current element that matches the given css selector. For a list of all supported selectors see the HTMLCollection section.
Arguments
string $selector
Css selector
ReturnObject HTML collection
Example
$col=& $document->getElementsBySelector("div.myclass"); //Find all divs with "myclass" class name
isSameNode
Return true if the given element and the current are the same node.
Arguments
object $element
Element for the comparison
ReturnBool True if the given element and the current are the same node, otherwise false
Example
$el1=& $document->getChildAt(0);
$el2=& $document->childNodes[0];
$check= $el1->isSameNode($el2); //true
cloneNode
Return a copy of the node
Arguments
bool $deep
If it's set to true child nodes are cloned too, otherwise(default) only the element is cloned
ReturnObject Copy of the node
Example
$copy= & $element->cloneNode();
hasChildNodes
Return true if the element has at least one child node
ReturnBool True if the element has at least one child node, otherwise false
Example
$element->hasChildNodes(); //false
$element->setInnerHTML("<div></div>");
$element->hasChildNodes(); //true
appendChild
Insert the given element at the end of the current element's child nodes array
Arguments
object $element
Element to insert
ReturnObject Reference to the current element
Example
$element=& $document->getElementById("element");
$document->body->appendChild($element); //Append the element to the body
appendTo
Insert the current element at the end of the given element's child nodes array
Arguments
object $element
Element in which to insert the current element
ReturnObject Reference to the current element
Example
$element=& $document->getElementById("element");
$element->appendTo($document->body); //Append the element to the body
prependChild
Insert the given element at the beginning of the current element's child nodes array
Arguments
object $element
Element to insert
ReturnObject Reference to the current element
Example
$element=& $document->getElementById("element");
$document->body->prependChild($element); //Prepend the element to the body
prependTo
Insert the current element at the beginning of the given element's child nodes array
Arguments
object $element
Element in which to insert the current element
ReturnObject Reference to the current element
Example
$element=& $document->getElementById("element");
$element->prependTo($document->body); //Prepend the element to the body
insertChildAt
Insert the given element at the specified position of the current element's child nodes array
Arguments
object $element
Element to insert
int $index
Zero based position index
ReturnObject Reference to the current element
Example
$element=& $document->getElementById("element");
$document->body->insertChildAt($element,1); //Insert the element in second position
insertAt
Insert the current element at the specified position of the given element's child nodes array
Arguments
object $element
Element in which to insert the current element
int $index
Zero based position index
ReturnObject Reference to the current element
Example
$element=& $document->getElementById("element");
$element->insertAt($document->body,2); //Insert the element in second position
insertBefore
Insert the second element before the first in the current element child nodes collection
Arguments
object $element
Before this element the second one will be inserted
object $node
Element to insert
ReturnObject Reference to the current element
Example
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$document->body->insertBefore($element,$node); //Insert the element before the second child of the body
insertBeforeNode
Insert the current element before the given one
Arguments
object $element
Before this element the current one will be inserted
ReturnObject Reference to the current element
Example
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$node->insertBeforeNode($element); //Insert the element before the second child of the body
insertAfter
Insert the second element after the first in the current element child nodes collection
Arguments
object $element
After this element the second one will be inserted
object $node
Element to insert
ReturnObject Reference to the current element
Example
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$document->body->insertAfter($element,$node); //Insert the element after the second child of the body
insertAfterNode
Insert the current element after the given one
Arguments
object $element
After this element the current one will be inserted
ReturnObject Reference to the current element
Example
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$node->insertAfterNode($element); //Insert the element after the second child of the body
replaceChild
Replace the first element with the second one in the current element child nodes array
Arguments
object $element
The element that must be replaced
object $replacement
The element used to replace the first one
ReturnObject Reference to the current element
Example
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$node->replaceChild($element,$node); //Replace the first child of the body with the element
replace
Replace the given element with the current one
Arguments
object $element
The element that must be replaced
ReturnObject Reference to the current element
Example
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$node->replace($element); //Replace the first child of the body with the element
removeChild
Remove the given element from the current element child nodes array
Arguments
object $element
The element that must be removed
ReturnObject Reference to the current element
Example
$element=& $document->body->childNodes[0];
$document->body->removeChild($element); //Remove the first child of the body
removeChildAt
Remove element at the given index from the current element child nodes array
Arguments
int $index
The zero based index of the element that must be removed
ReturnObject Reference to the current element
Example
$document->body->removeChild(0); //Remove the first child of the body
removeChildAt
Remove the current element from its parent node child nodes array
ReturnObject Reference to the current element
Example
$document->body->childNodes[0]->remove(); //Remove the first child of the body
emptyChildNodes
Remove every child node from the current element
ReturnObject Reference to the current element
Example
$document->body->hasChildNodes(); //True
$document->body->emptyChildNodes(); //Remove the first child of the body
$document->body->hasChildNodes(); //False