0

I have

<img alt="text" src="source" />

I'd like to get this alt to one var in JavaScript. It'll be better if it won't be necessary to use jQuery.

Please for help :)

  • Duplicate of [Get all Attributes from a HTML element with Javascript/jQuery](https://stackoverflow.com/q/2048720/691711) – zero298 Feb 11 '21 at 19:08
  • 2
    Does this answer your question? [Get all Attributes from a HTML element with Javascript/jQuery](https://stackoverflow.com/questions/2048720/get-all-attributes-from-a-html-element-with-javascript-jquery) – Semih Arslanoglu Feb 11 '21 at 19:13

2 Answers2

0

You could do like this:

document.getElementById('imgId').getAttribute('alt')
0

can get any attribute by .getAttribute

i.e.

imgAlt.getAttribute("alt")

imgAlt.getAttribute("title")

const imgAlt = document.getElementById("myImg");

console.log(imgAlt.getAttribute("alt"));
<img id="myImg" alt="text" src="source" />
GMKHussain
  • 3,342
  • 1
  • 21
  • 19