JavaScript’s Element.hasAttributes()
method returns a Boolean value indicating whether the Element
has any attributes or not.
Syntax
Element.hasAttributes();
Parameters
None.
Return value
A Boolean value that is true
if the element has attributes and false
if the element has no attributes.
Examples
Here is an example of using the JavaScript hasAttributes()
method.
Consider the following line of HTML:
<a href="https://javascriptguide.com" target="_blank">JavaScript Guide</a>
How can we make sure that the a
element has attributes with JavaScript?
Well, we can select the element with the querySelector()
method and use the hasAttributes()
method:
let a = document.querySelector('a');
console.log(a.hasAttributes());
You should now be able to see the return value of our method call in the console of your browser.
Browser compatibility
Chrome
|
Edge
|
Firefox
|
Internet Explorer
|
Opera
|
Safari
|
Chrome (Android)
|
Firefox (Android)
|
Opera (Android)
|
Safari (iOS)
|
Samsung Internet
|
WebView (Android)
|
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
hasAttributes() |
1 | 12 | 1 | 8 | 12.1 | 1 | 18 | 4 | 12.1 | 1 | 1.0 | 4.4 |