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="">
   let img = document.querySelector('img[alt="text"]')
Try it Yourself »