Definition and Usage
Basic Example
<video src="video.mp4" autoplay></video>
const myVideo = document.querySelector("video");
/* Returns the attribute value as a string. */
console.log(myVideo.getAttribute("src")); // "video.mp4"
/* Returns an empty string ("") if the attribute exists but has no value. */
console.log(myVideo.getAttribute("autoplay")); // ""
/* Returns null if the specified attribute does not exist on the element. */
console.log(myVideo.getAttribute("loop")); // null
Code Explanation
The querySelector() function returns the first element that matches the specified CSS selector within the HTML document or a specific element.
Syntax
element.getAttribute(attributeName)
Parameters
attributeName |
Required. The name of the attribute whose value to retrieve (a string). |
|---|
Return Value
However, there is a separate hasAttribute() function specifically designed to check whether an element has a specified attribute.
This function returns true if the specified attribute exists on the element; otherwise, it returns false.
To check whether a specified attribute exists on a DOM element, use the hasAttribute() function.
Things to Keep in Mind
There are a few things to keep in mind when using the getAttribute() function.
When an Attribute Has Multiple Values
Strictly speaking, these are multiple class names rather than multiple values.
However, in this guide, they will be referred to as "values," as that is the term commonly used.
<p class="a b">The class attribute has the values a and b.</p>
const elem = document.querySelector("p");
const elemClassValue = elem.getAttribute("class");
console.log(elemClassValue); // "a b" => returns the full string including spaces
if (elemClassValue === "a") {
console.log("The element has the 'a' class value.");
} else {
console.log("The element does not have the 'a' class value.");
}
// Output: "The element does not have the 'a' class value."
As shown in the example above, the getAttribute() function returns the value of an attribute as a single string, including spaces.
In this case, the output will be "The element does not have the 'a' class value."
The values of attributes such as class, rel, and role can contain multiple values separated by spaces.
When checking the values of these attributes, you should either use a different function, or if you need to use the getAttribute() function, you must split the string by spaces and analyze it (for example, using the split() function) before using it.
The following is an example that uses classList.contains() instead of the getAttribute() function to check whether a specific value is included in the class attribute.
Additional Explanation
The classList.contains() method checks whether the specified class attribute value exists in a DOM element. It returns true if the value exists and false otherwise.
classList.contains() Instead of getAttribute() to Check for a Specific class Value
const elem = document.querySelector("p");
if (elem.classList.contains("a")) {
console.log("The element has the 'a' class value.");
} else {
console.log("The element does not have the 'a' class value.");
}
// Output: "The element has the 'a' class value."
The following is an example that uses the split() function to split a string by spaces when it is necessary to use the getAttribute() function.
Additional Explanation
The split() function splits a given string based on a specified delimiter or a regular expression and returns a new array.
split() Function to Split a String by Spaces When the getAttribute() Function Is Required
const elem = document.querySelector("p");
const elemClassValue = elem.getAttribute("class");
console.log(elemClassValue); // "a b"
const elemClassValueArray = elemClassValue.split(" "); // ["a", "b"]
let isClassName = false;
elemClassValueArray.forEach(classname => {
if (classname === "a") {
isClassName = true;
}
});
console.log(isClassName); // true
Do Not Use the nonce Attribute
The nonce attribute is one of the important security-related attributes. It is mainly used in relation to Content Security Policy (CSP), a security policy for scripts and stylesheets. When the value of this attribute is retrieved using the getAttribute() function, it returns an empty string ("").
const nonceAttrValue = script.getAttribute("nonce");
/* returns an empty string ("") */
console.log(nonceAttrValue); // ""
Browser Compatibility
| Method |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
getAttribute()
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
getAttribute()
|
DOM Standard #ref-for-dom-element-getattribute① |
References
See Also
- JavaScript setAttribute() Function – Setting or Adding Attributes to Elements
- JavaScript removeAttribute() Function – Removing Specified Attributes from Elements
- JavaScript hasAttribute() Function – Checking If an Element Has an Attribute
- JavaScript toggleAttribute() Function – Toggling a Boolean Attribute on an Element
- JavaScript classList.add() – Add Class Values to an Element