Latest web development tutorials
 

Image alt Property

< Image Object

Example

Return the alternate text of an image:

var x = document.getElementById("myImg").alt;

The result of x will be:

Crazy computerman
Try it Yourself »

Definition and Usage

The alt property sets or returns the value of the alt attribute of an image.

The required alt attribute specifies an alternate text for an image, if the image for some reason cannot be displayed (because of slow connection, an error in the src attribute, or if the user uses a screen reader).

Note: Internet Explorer displays the value of the alt attribute as a tooltip when mousing over the <img> element. This is NOT the correct behavior, according to the HTML specification. All other browsers are following the specification, and will only display the alt text if the image cannot be displayed.

Tip: If you want to create a tooltip for an image, use the global title attribute.


Browser Support

Property
alt Yes Yes Yes Yes Yes

Syntax

Return the alt property:

imageObject.alt

Set the alt property:

imageObject.alt=text

Property Values

Value Description
text Specifies an alternate text for an image.

Guidelines for the alt text:
  • The text should describe the image if the image contains information
  • The text should explain where the link goes if the image is inside an <a> element
  • Use alt="" if the image is only for decoration

Technical Details

Return Value: A String, representing the alternate text of the image

More Examples

Example

Change the alternate text of an image:

document.getElementById("myImg").alt="newAlternateText";
Try it Yourself »

Related Pages

HTML reference: HTML <img> alt attribute


< Image Object