Adding alt attribute to image in javascript

mukund picture mukund · Mar 18, 2013 · Viewed 21.9k times · Source

I want to know how to add alt attribute to an image in javascript, below is my code...

var image = document.createElement('img');
image.src='../../KY/images/common/buttons/browseIcon.png';

Thanks in advance.

Answer

Bigood picture Bigood · Mar 18, 2013

You have two ways : you can use either alt property

image.alt = "Your text here"

or setAttribute method

image.setAttribute("alt","Your text here");