The following code shows an Example of hasAttributes() Method in JavaScript.
Programmingempire
Illustrating an Example of hasAttributes() Method in JavaScript
As can be seen, the hasAttribute() method returns a true value since the image element has attributes.
<html>
<head>
<title>hasAttributes Method Demo</title>
<script>
function f1()
{
var v1=document.getElementById("d2");
var x1=document.getElementById("im1");
str="The image shown above has attributes: "+x1.hasAttributes();
str1=v1.innerHTML;
str1+="<br/>"+str;
v1.style.width='300px';
v1.style.display='block';
v1.innerHTML=str1;
}
</script>
<style>
.c1{
border: 4px solid #33ff99;
border-radius: 10px;
background-color: #5566aa;
color: #aaffff;
font-size: 20px;
text-align: center;
margin:5px;
padding: 10px;
}
.a1{
margin: 50px;
}
.c2{
background-color: #ddeeff;
color: #ff5566;
font-size: 25px;
text-align: left;
margin:40px;
padding: 5px;
width: 400px;
}
</style>
</head>
<body>
<div id="d1" class="c2"><br/><br/>
Example of hasAttributes() Method<br/>
<br/><br/>
<img id="im1" src="j4.jpg" alt="My Pet" width="200" height="200" style="border: 2px red solid;"/>
<br/><br/>
<button id="b7" class="c1" onclick="f1()">Image Attributes</button>
<br/><br/>
<div class="c2" id="d2" style="display:none;"></div>
</div>
</body>
</html>
Output
Further Reading
Evolution of JavaScript from ES1 to ES2020
Introduction to HTML DOM Methods in JavaScript