"); } ``` ### Output ```text COOKING CHILDREN WEB WEB ``` --- ## Key Considerations 1. **Case Sensitivity**: In XML DOM, attribute names are **case-sensitive**. You must pass the exact casing of the attribute name to `getAttribute()`. In HTML DOM, attribute names are treated as case-insensitive. 2. **Non-existent Attributes**: According to modern DOM specifications, if the attribute does not exist on the element, `getAttribute()` returns `null`. It is good practice to check for `null` before processing the returned value: ```javascript var category = element.getAttribute('category'); if (category !== null) { // Process the attribute value } ``` 3. **Difference from Property Access**: In HTML DOM, accessing a property directly (e.g., `element.id`) might yield different results than `element.getAttribute('id')` for certain attributes (like `src` or `href`), where the property returns a resolved absolute URL, while `getAttribute()` returns the exact literal string value defined in the markup.
Dom Met Element Getattribute
## XML DOM Element: getAttribute() Method
The `getAttribute()` method returns the value of a specified attribute on an element. If the specified attribute does not exist, it returns `null` (or an empty string `""` in some older browsers).
This method is highly useful when parsing XML documents or manipulating HTML DOM elements dynamically, allowing you to retrieve metadata, identifiers, or custom data attributes.
---
## Syntax
```javascript
elementNode.getAttribute(name)
```
### Parameters
| Parameter | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| `name` | String | **Yes** | The name of the attribute whose value you want to retrieve. |
### Return Value
* **String**: The value of the specified attribute.
* **Null / Empty String**: Returns `null` (or `""` in legacy environments) if the attribute does not exist on the element.
---
## Code Example
The following example demonstrates how to load an XML document (`books.xml`) using a helper function `loadXMLDoc()` and retrieve the value of the `category` attribute from all `` elements.
### The XML Source (`books.xml`)
```xml
Everyday Italian
Giada De Laurentiis
2005
30.00
Harry Potter
J K. Rowling
2005
29.99
XQuery Kick Start
James McGovern
2003
49.99
Learning XML
Erik T. Ray
2003
39.95
```
### JavaScript Implementation
```javascript
// Load the XML document
var xmlDoc = loadXMLDoc("books.xml");
// Get all elements
var books = xmlDoc.getElementsByTagName('book');
// Loop through each book and print its 'category' attribute
for (var i = 0; i < books.length; i++) {
var categoryValue = books.getAttribute('category');
document.write(categoryValue + "
"); } ``` ### Output ```text COOKING CHILDREN WEB WEB ``` --- ## Key Considerations 1. **Case Sensitivity**: In XML DOM, attribute names are **case-sensitive**. You must pass the exact casing of the attribute name to `getAttribute()`. In HTML DOM, attribute names are treated as case-insensitive. 2. **Non-existent Attributes**: According to modern DOM specifications, if the attribute does not exist on the element, `getAttribute()` returns `null`. It is good practice to check for `null` before processing the returned value: ```javascript var category = element.getAttribute('category'); if (category !== null) { // Process the attribute value } ``` 3. **Difference from Property Access**: In HTML DOM, accessing a property directly (e.g., `element.id`) might yield different results than `element.getAttribute('id')` for certain attributes (like `src` or `href`), where the property returns a resolved absolute URL, while `getAttribute()` returns the exact literal string value defined in the markup.
"); } ``` ### Output ```text COOKING CHILDREN WEB WEB ``` --- ## Key Considerations 1. **Case Sensitivity**: In XML DOM, attribute names are **case-sensitive**. You must pass the exact casing of the attribute name to `getAttribute()`. In HTML DOM, attribute names are treated as case-insensitive. 2. **Non-existent Attributes**: According to modern DOM specifications, if the attribute does not exist on the element, `getAttribute()` returns `null`. It is good practice to check for `null` before processing the returned value: ```javascript var category = element.getAttribute('category'); if (category !== null) { // Process the attribute value } ``` 3. **Difference from Property Access**: In HTML DOM, accessing a property directly (e.g., `element.id`) might yield different results than `element.getAttribute('id')` for certain attributes (like `src` or `href`), where the property returns a resolved absolute URL, while `getAttribute()` returns the exact literal string value defined in the markup.
YouTip