I am working on a site that has a page that will have a couple hundred thumbnails. I would like to have the filenames (the src) of the images populate the alt fields. So for example, I currently have the thumbnails as follows:
<img src="images/thumb1.jpg" />
I would like to populate the alt fields with the filename. So, the desired result would be:
<img src="images/thumb1.jpg" alt="thumb1" />
Is there a way I can automatically generate these alt tags using the images src?
Any suggestions are appreciated. Thank you for the help!
An untested, first guess, would be:
JS Fiddle demo.
Just to demonstrate how much easier this can be, with a JavaScript library, I thought I’d also offer the jQuery demo too:
jQuery-based JS Fiddle demo.
Edited because I’m an idiot…
I forgot to point out that you’ll need to wait for the window to finish loading (or, at least, for the document.ready event), so try it this way:
And change the opening
bodytag to:JS Fiddle demo.
Edited to address the OP’s final question:
JS Fiddle, though I suspect there’s a far more concise way…