XML DOM hasAttribute() method
Definition and usage
If the current element node has the specified attribute, it returns true; otherwise, it returns false.
Syntax:
hasAttribute(name)
Parameter | Description |
---|---|
name | Required. Specifies the attribute to be retrieved. |
Description
This method determines whether an element has the specified attribute without returning 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 the attribute, the hasAttribute() method will return true.
Example
In all examples, we will use XML files 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:
true