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.

0

3 Answers 3

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");
Sign up to request clarification or add additional context in comments.

1 Comment

"image.setAttribute("alt","Your text here");" worked perfectly for what I needed. thanks!
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")

Comments

0

You could just use the alt property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.