Help Center

Local Navigation

Object: Element

Availability

BlackBerry® Device Software version 4.6 or later

Extends

Node

Extended by

HTMLElement

The Element object represents an element in an HTML or XML document. Elements may have attributes associated with them. You can use the Node.attributes property to retrieve a map of all the attributes attached to the element.

This object provides several methods for handling all the available attributes, individual attribute values, or individual Attr objects.

Element properties

Property name

Type

Description

Status

Availability

tagName

String

The name of the element.

read only

4.6 or later

Element methods

Method name

Description

Availability

getAttribute()

Retrieves the value of the attribute of the given name.

4.6 or later

getAttributeNode()

Retrieves the attribute node of the given name.

4.6 or later

getAttributeNodeNS()

Retrieves the attribute node with the given namespace URI and local name.

4.6 or later

getAttributeNS()

Retrieves the value of the attribute with the given namespace URI and local name.

4.6 or later

getElementsByTagName()

Returns a NodeList object that contains an array of an element’s descendent element nodes with the specified tag name.

4.6 or later

getElementsByTagNameNS()

Returns a NodeList object that contains an array of an element’s descendent element nodes with the given local name and namespace URI.

4.6 or later

hasAttribute()

Determines whether an attribute of the given name is specified on the element.

4.6 or later

hasAttributeNS()

Determines whether an attribute of the given namespace and local name is specified on the element.

4.6 or later

removeAttribute()

Removes the attribute of the given name from the element node.

4.6 or later

removeAttributeNode()

Removes the specified attribute node from the element node.

4.6 or later

removeAttributeNS()

Removes the attribute node with the given namespace URI and local name from the element node.

4.6 or later

setAttribute()

Adds a new attribute to an element, and sets it to the given value.

4.6 or later

setAttributeNode()

Adds a new attribute node to an element.

4.6 or later

setAttributeNodeNS()

Adds a new attribute node to an element.

4.6 or later

setAttributeNS()

Adds a new attribute to an element, and sets it to the given value.

4.6 or later

Method: Element.getAttribute()

Availability

BlackBerry® Device Software version 4.6 or later

The getAttribute() method retrieves the value of the Attr with the given name.

Syntax

Element.getAttribute( name )

Parameters

Parameter

Type

Description

name

String

The name of the attribute for which to retrieve the value.

Return values

Returns the Node.nodeValue property of the Attr object as a string, or the empty string if that attribute has no specified or default value.

Exceptions

None.

Method: Element.getAttributeNode()

Availability

BlackBerry® Device Software version 4.6 or later

The getAttributeNode() method retrieves the attribute node of the given name.

Unlike Element.getAttribute(), this method allows you to access the Attr object without having to iterate over the Node.attributes property.

Syntax

Element.getAttributeNode( name )

Parameters

Parameter

Type

Description

name

String

The name of the Attr node to retrieve.

Return values

Returns the Attr object with a Node.nodeName property of name, or a value of null the specified attribute doesn’t exist.

Exceptions

None.

Method: Element.getAttributeNodeNS()

Availability

BlackBerry® Device Software version 4.6 or later

The getAttributeNodeNS() method retrieves the Attr node with the given namespace URI and local name.

Syntax

Element.getAttributeNodeNS( namespaceURI, localName )

Parameters

Parameter

Type

Description

namespaceURI

String

The namespace of the attribute to retrieve. Specifying a value of null makes this method functionally equivalent to Element.getAttributeNode().

localName

String

The local name of the attribute to retrieve.

Return values

Returns the Attr object with the given namespace URL and local name, or a value of null the specified attribute doesn’t exist.

Exceptions

Exception

Description

NOT_SUPPORTED_ERR

This error is thrown if the document does not support XML namespaces.

See also

Attr

Method: Element.getAttributeNS()

Availability

BlackBerry® Device Software version 4.6 or later

The getAttributeNS() method retrieves the value of the Attr with the given namespace URI and local name.

Syntax

Element.getAttributeNS( namespaceURI, localName )

Parameters

Parameter

Type

Description

namespaceURI

String

The namespace of the attribute for which to retrieve the value. Specifying a value of null makes this method functionally equivalent to Element.getAttribute().

localName

String

The local name of the attribute for which to retrieve the value.

Return values

Returns the Node.nodeValue property of the Attr object as a string, or the empty string if that attribute has no specified or default value.

Exceptions

Exception

Description

NOT_SUPPORTED_ERR

This error is thrown if the document does not support XML namespaces.

See also

Attr

Method: Element.getElementsByTagName()

Availability

BlackBerry® Device Software version 4.6 or later

The getElementsByTagName() method returns a NodeList object that contains an array of an element’s descendent Element nodes with the specified tag name.

This method is identical to Document.getElementsByTagName(), except that it allows you to search a smaller subset of Element nodes.

Syntax

Element.getElementsByTagName( tagName )

Parameters

Parameter

Type

Description

tagName

String

The name of the tag to match on. Specify a value of “*” to match all elements.

Return values

Returns a new NodeList object containing an array of all the elements that match tagName, in the order in which they are encountered in a preorder traversal of the Document tree.

Exceptions

None.

Method: Element.getElementsByTagNameNS()

Availability

BlackBerry® Device Software version 4.6 or later

The getElementsByTagNameNS() method returns a NodeList object that contains an array of an element’s descendent Element nodes with the given local name and namespace URI.

This method is identical to Document.getElementsByTagNameNS(), except that it allows you to search a smaller subset of Element nodes.

Syntax

Element.getElementsByTagNameNS( namespaceURI, localName )

Parameters

Parameter

Type

Description

namespaceURI

String

The namespace of the elements to match on. Specify a value of “*” to match all elements.

Specifying a value of null makes this method functionally equivalent to Element.getElementsByTagName().

localName

String

The local name of the elements to match on (that is, the qualified name minus the prefix). Specify a value of “*” to match all local elements.

Return values

Returns a new NodeList object containing an array of all the elements with a given local name and namespace URI, in the order in which they are encountered in a preorder traversal of the Document tree.

Exceptions

Exception

Description

NOT_SUPPORTED_ERR

This error is thrown if the document does not support XML namespaces.

Method: Element.hasAttribute()

Availability

BlackBerry® Device Software version 4.6 or later

The hasAttribute() method determines whether an attribute of the given name is specified on the element. You can use this method to verify that an attribute is attached to an element before retrieving it.

Syntax

Element.hasAttribute( name )

Parameters

Parameter

Type

Description

name

String

The name of the attribute to check.

Return values

Returns:

  • true, if the specified attribute exists on the element
  • false, if the attribute does not exist

Exceptions

None.

Method: Element.hasAttributeNS()

Availability

BlackBerry® Device Software version 4.6 or later

The hasAttributeNS() method determines whether an attribute of the given namespace and local name is specified on the element. You can use this method to verify that an attribute is attached to an element before retrieving it.

Syntax

Element.hasAttributeNS( namespaceURI, localName )

Parameters

Parameter

Type

Description

namespaceURI

String

The namespace of the attribute to check.

localName

String

The local name of the attribute to check.

Return values

Returns:

  • true, if the specified attribute exists on the element
  • false, if the attribute does not exist

Exceptions

Exception

Description

NOT_SUPPORTED_ERR

This error is thrown if the document does not support XML namespaces.

Method: Element.removeAttribute()

Availability

BlackBerry® Device Software version 4.6 or later

The removeAttribute() method removes the Attr node of the given name from the element node.

If you are removing an attribute from the Element object that is known to have a default value, the attribute will immediately be reattached to the element with its default value, as well as its corresponding namespace URI, local name, and prefix, if applicable.

If the attribute has no specified or default value, this method has no effect.

Syntax

Element.removeAttribute( name )

Parameters

Parameter

Type

Description

name

String

The name of the Attr node to remove.

Return values

None.

Exceptions

Exception

Description

NO_MODIFICATION_ALLOWED_ERR

This error is thrown if the Element node is read only.

NOT_FOUND_ERR

This error is thrown if no node exists in the Element with a Node.nodeName value of name.

See also

Attr

Method: Element.removeAttributeNode()

The removeAttributeNode() method removes the specified Attr node from the element node.

If you are removing an attribute from the Element object that is known to have a default value, the attribute will immediately be reattached to the element with its default value, as well as its corresponding namespace URI, local name, and prefix, if applicable.

Syntax

Element.removeAttributeNode( oldAttr )

Parameters

Parameter

Type

Description

oldAttr

Attr

The Attr node to remove from the attribute list.

Return values

None.

Exceptions

Exception

Description

NO_MODIFICATION_ALLOWED_ERR

This error is thrown if the Element node is read only.

NOT_FOUND_ERR

This error is thrown if the oldAttr node does not exist in the Element.

See also

Attr

Method: Element.removeAttributeNS()

Availability

BlackBerry® Device Software version 4.6 or later

The removeAttributeNS() method removes the Attr node with the given namespace URI and local name from the element node.

If you are removing an attribute from the Element object that is known to have a default value, the attribute will immediately be reattached to the element with its default value, as well as its corresponding namespace URI, local name, and prefix, if applicable.

If the attribute has no specified or default value, this method has no effect.

Syntax

Element.removeAttributeNS( namespaceURI, localName )

Parameters

Parameter

Type

Description

namespaceURI

String

The namespace of the attribute to remove.

localName

String

The local name of the attribute to remove.

Return values

None.

Exceptions

Exception

Description

NO_MODIFICATION_ALLOWED_ERR

This error is thrown if the Element node is read only.

NOT_SUPPORTED_ERR

This error is thrown if the document does not support XML namespaces.

See also

Attr

Method: Element.setAttribute()

Availability

BlackBerry® Device Software version 4.6 or later

The setAttribute() method adds a new Attr node to an element, and sets it to the given value. If an attribute with the given name already exists on the Element node, its value is changed to the given value.

This method only sets values as text strings, not entities. If the value contains any markup syntax (for example, if the value is an EntityReference), it will not be parsed and it must be properly escaped. If you want to assign values containing entities, you should use Element.setAttributeNode() or Element.setAttributeNodeNS().

Syntax

Element.setAttribute( name, value )

Parameters

Parameter

Type

Description

name

String

The name of the attribute to create or modify.

value

String

The value to set the attribute to, in string form.

Return values

None.

Exceptions

Exception

Description

INVALID_CHARACTER_ERR

This error is thrown if name contains an illegal character.

NO_MODIFICATION_ALLOWED_ERR

This error is thrown if the Element node is read only.

Method: Element.setAttributeNode()

Availability

BlackBerry® Device Software version 4.6 or later

The setAttributeNode() method adds a new Attr node to an element. If an attribute node with the same name as the new Attr node already exists on an element, that Attr node is replaced.

Unlike Element.setAttribute(), this method allows you to add DOM nodes, instead of specifying separate arguments for the attribute name and value. Note that your attribute nodes cannot be reused in multiple elements; you must either clone the Attr node, or remove an Attr node from another element before adding it to a new element.

Syntax

Element.setAttributeNode( newAttr )

Parameters

Parameter

Type

Description

newAttr

Attr

The Attr node to add to the attribute list.

Return values

If newAttr replaces an existing attribute, this method returns the replaced Attr object; otherwise it returns null.

Exceptions

Exception

Description

INUSE_ATTRIBUTE_ERR

This error is thrown if newAttr is already an attribute of another Element object. To add an attribute used elsewhere, you must clone it, and then add it.

NO_MODIFICATION_ALLOWED_ERR

This error is thrown if the Element object is read only.

WRONG_DOCUMENT_ERR

This error is thrown if newAttr was created from a different document than the one that created this Element object.

Method: Element.setAttributeNodeNS()

Availability

BlackBerry® Device Software version 4.6 or later

The setAttributeNodeNS() method adds a new Attr node to an element. If an Attr node with the same namespace and local name as the new Attr node already exists on an element, that attribute node is replaced.

Unlike Element.setAttributeNS(), this method allows you to add DOM nodes, instead of specifying separate arguments for the attribute name and value. Note that your attribute nodes cannot be reused in multiple elements; you must either clone the Attr node, or remove an Attr node from another element before adding it to a new element.

Syntax

Element.setAttributeNodeNS( newAttr )

Parameters

Parameter

Type

Description

newAttr

Attr

The Attr node to add to the attribute list.

Return values

If newAttr replaces an existing attribute, this method returns the replaced Attr object; otherwise it returns null.

Exceptions

Exception

Description

INUSE_ATTRIBUTE_ERR

This error is thrown if newAttr is already an attribute of another Element object. To add an attribute used elsewhere, you must clone it, and then add it.

NO_MODIFICATION_ALLOWED_ERR

This error is thrown if the Element object is read only.

NOT_SUPPORTED_ERR

This error is thrown if the document does not support XML namespaces.

WRONG_DOCUMENT_ERR

This error is thrown if newAttr was created from a different document than the one that created this Element object.

Method: setAttributeNS()

Availability

BlackBerry® Device Software version 4.6 or later

The setAttributeNS() method adds a new Attr node to an element, and sets it to the given value. If an attribute with the given namespace and local name already exists on the Element node, its prefix is change to the prefix portion of the qualified name, and its value is changed to the given value.

This method only sets values as text strings, not entities. If the value contains any markup syntax (for example, if the value is an EntityReference), it will not be parsed and it must be properly escaped. If you want to assign values containing entities, you should use Element.setAttributeNode() or Element.setAttributeNodeNS().

Syntax

Element.setAttributeNS( namespaceURI, qualifiedName, value )

Parameters

Parameter

Type

Description

newAttr

String

The namespace of the attribute to create or modify.

qualifiedName

String

The qualified name of the attribute to create or modify.

value

String

The value of the attribute, in string form.

Return values

Returns the Node.nodeValue property of the Attr object as a string, or the empty string if that attribute has no specified or default value.

Exceptions

Exception

Description

INVALID_CHARACTER_ERR

This error is thrown if name contains an illegal character.

NAMESPACE_ERR

This error is thrown if:

  • qualifiedName is malformed
  • qualifiedName has a prefix, but namespaceURI is null
  • prefix is xml, but namespaceURI is not http://www.w3.org/XML/1998/namespace
  • qualifiedName is xmlns, but namespaceURI is not http://www.w3.org/2000/xmlns/

NO_MODIFICATION_ALLOWED_ERR

This error is thrown if the Element node is read only.

NOT_SUPPORTED_ERR

This error is thrown if the document does not support XML namespaces.

Next topic: Object: Entity

Was this information helpful? Send us your comments.