How to get element by alt attribute in JavaScript

In this article, you will learn how you can get the element by alt attribute using JavaScript. Sometimes, we can't add any class or id attributes to an element. So,in that case, we can easily get that element by using its alt attribute.

get elements by alt attribute
  <img alt="text" src="https://app.3schools.in/logo.png">
  <script>
   let img = document.querySelector('img[alt="text"]')
   alert(img.alt);
  </script>
Try it Yourself »