10

I was just wondering that in the html img tag

<img alt = "default image" src = "image"> is it possible to have a alternate image rather than just text. Or if is it possible to nest the img tag in the alt ""s.

Hammad Ahmed
  • 271
  • 2
  • 4
  • 17
  • unsure if it would be possible using pure HTML. Some scripting may be required. – Adjit Feb 13 '15 at 17:03
  • Alternative under what conditions? The `alt` attribute is primarily meant to be useful when the user does not see images at all, for one reason or another, so that a *textual* alternative is presented. – Jukka K. Korpela Feb 13 '15 at 21:22
  • I wanted a pictorial alternative than a textual alternative but I've got the answer using the `onError` JavaScript function. – Hammad Ahmed Feb 14 '15 at 03:00

2 Answers2

20

No, you can't put another image in the alt attribute of an <img>. You can however use the onerror event to load another image using JavaScript, so if the original image doesn't load, a backup can be loaded.

function standby() {
    document.getElementById('foo').src = 'https://www.google.com/images/srpr/logo11w.png'
}
<img id="foo" src="notfound.gif" onerror="standby()">
j08691
  • 204,283
  • 31
  • 260
  • 272
0

As far as I know, the alt attribute is supposed to have only text, since it is helpful for screen readers to "read" images.

If what you are searching for is a alternate image when the main one fails, you can do so with some JavaScript.

On this question you can see how to use the onError usage:

jQuery/JavaScript to replace broken images

Community
  • 1
  • 1
Moz
  • 117
  • 4