Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4549668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:23:32+00:00 2026-05-21T16:23:32+00:00

I am working on a site that has a page that will have a

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:23:32+00:00Added an answer on May 21, 2026 at 4:23 pm

    An untested, first guess, would be:

    var images = document.getElementsByTagName('img');
    var numImages = images.length;
    
    for (i=0; i<numImages; i++) {
        images[i].alt = images[i].src;
    }
    

    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:

    $('img').each(
        function(){
            this.alt = this.src;
            this.title = this.src;
        });
    

    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:

    function makeAlt() {
        var images = document.getElementsByTagName('img');
        var numImages = images.length;
        for (i = 0; i < numImages; i++) {
            images[i].alt = images[i].src;
            images[i].title = images[i].src;
        }
    }
    

    And change the opening body tag to:

    <body onload="makeAlt">
    

    JS Fiddle demo.


    Edited to address the OP’s final question:

    function makeAlt() {
        var images = document.getElementsByTagName('img');
        var numImages = images.length;
        var newAlt, stopAt;
        for (i = 0; i < numImages; i++) {
            newAlt = images[i].src.split('/').pop();
            stopAt = newAlt.indexOf('.');
            newAlt = newAlt.substring(0,stopAt);
            images[i].alt = newAlt;
            images[i].title = newAlt;
        }
    }
    

    JS Fiddle, though I suspect there’s a far more concise way…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a site that has a splash page and I want it
I'm working on a site that has a requirement to share session between a
I'm working on a site that has about 3,000-4,000 dynamically generated pages, and I'm
I'm working on a site that still has a lot of scripts in ColdFusion.
I am working on a medium size web site that has plenty of custom
I've been working on an internal site that has Apache / PHP running. I
I'm working on a database site now that has the URL structure: example.com/f.php?id=12 I'd
An application I'm working with has the page structure setup such so that when
I am working on a site that will allow users to create an account.
I am working on a social networking site and it has a wall like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.