XML DOM hasAttribute() Method
Definition and Usage
Returns true if the current element node has the specified attribute, otherwise returns false.
Syntax:
hasAttribute(name)
Parameter | Description |
---|---|
name | Required. Specifies the attribute to be retrieved. |
Description
This method determines whether an element has a specified attribute but does not return the value of that attribute. Note that if the specified attribute is explicitly set in this document, or if the document type declaration sets a default value for this attribute, the hasAttribute() method returns true in both cases.
Example
In all examples, we will use the XML file books.xml, and JavaScript functions loadXMLDoc().
The following code snippet checks whether the first <book> element in "books.xml" has the "category" attribute:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
document.write(x.hasAttribute("category")
);
The output of the above code is:
true