Adding alt attribute to image in javascript
Asked Answered
H

3

10

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.

Hayne answered 18/3, 2013 at 7:28 Comment(0)
B
27

You have two ways : you can use either alt property

image.alt = "Your text here"

or setAttribute method

image.setAttribute("alt","Your text here");
Blockhouse answered 18/3, 2013 at 7:30 Comment(1)
"image.setAttribute("alt","Your text here");" worked perfectly for what I needed. thanks!Cristoforo
S
1

Above mentioned methods would not work when u create buttons or any other element in dom javascript, and for this use the below mentioned method

control.setAttribute("title","your tooltip value")
Sphalerite answered 28/12, 2013 at 15:18 Comment(0)
C
0

You could just use the alt property.

Chunchung answered 18/3, 2013 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.