Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu June 03, 2023 . #alt . #Attribute

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 access that element by using its alt attribute.

Example : get img element by alt attribute. #
<img alt="text" src="https://app.3schools.in/logo.png">
<script>
 let img = document.querySelector('img[alt="text"]')
 console.log(img.alt);
</script>
Try it Yourself »
save
listen
AI Answer
Write Your Answer
loading
back
View All