• 0

Javascript Alt to Caption


Question

I am workin on a script that will read in alt attributes of images and spit them out as the caption automatically, but apparently i'm not doin the for loop right because it's just display all the captions at the bottom, not after each individual image.

Right now it displays all the images, then all the captions. I need it to display 1 image, then it's caption ...and then continue through the page.

Also, but fairly minor: How can I get rid of the line break after "Caption: " before the actual Caption text?

Can anyone give me any pointers?

Here is the JS:

window.onload = function extractImageAlts()
{
	images = document.getElementsByTagName('img');
	for (var i = 0; i < images.length; i++)
		{
		var title = images[i].getAttribute('alt');
		if ((title) && (title != ''))
			{
				var newdiv = document.createElement('div');
				newdiv.className = 'caption';

				var para = document.createElement("p");
				newdiv.appendChild(para);
				var txt = document.createTextNode("Caption: ");
				para.appendChild(txt);

				newdiv.appendChild(document.createTextNode(title));
				images[i].parentNode.appendChild(newdiv);						
			}
		}
}

Here is the XHTML:

<!DOCTYPE html PULIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">;html xmlns="http://w3.org/1999/xhtml" xml:lang="en">

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>Gallery</title>

  &lt;script type="text/javascript" src="altCaption.js"></script>t;/head&gt;



&lt;body&gt;

	&lt;img src="https://static.flickr.com/47/130388608_65c2fd0b82.jpg" title="Our Table" alt="THIS IS MY 1st ALT ATTRIBUTE"&gt;

	&lt;br /&gt;

	&lt;img src="https://static.flickr.com/47/130388608_65c2fd0b82.jpg" title="Our Table" alt="THIS IS MY 2nd ALT ATTRIBUTE"&gt;

	&lt;br /&gt;

	&lt;img src="https://static.flickr.com/47/130388608_65c2fd0b82.jpg" title="Our Table" alt="THIS IS MY 3rd ALT ATTRIBUTE"&gt;

&lt;/body&gt;


&lt;/html&gt;

post-32407-1147634801_thumb.jpg

Link to comment
https://www.neowin.net/forum/topic/461063-javascript-alt-to-caption/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

my friend you rock :yes: :punk:

Here is (hopefully) my last question:

How can I target just the images in my "content" div for example? I want to apply this alt attribute --> Caption script only to images that are inside my "content" div, not the entire page. :blush: Thanks so much for all the wonderful help.

  • 0
  xxdesmus said:

my friend you rock :yes: :punk:

Here is (hopefully) my last question:

How can I target just the images in my "content" div for example? I want to apply this alt attribute --> Caption script only to images that are inside my "content" div, not the entire page. :blush: Thanks so much for all the wonderful help.

  images = document.getElementsByTagName('img');

==>

  images = document.getElementById('content').getElementsByTagName('img');

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Floorp 11.29.0 by Razvan Serea Floorp is a cutting-edge web browser that combines the trusted foundation of Mozilla's Firefox with a unique Japanese perspective, offering users an exceptional online experience. This open-source browser prioritizes privacy, customization, and security. Floorp is transparent, with no user tracking or data sharing, and it's completely open source. With a strict no-tracking policy and full transparency, your personal information remains private. As an open-source project, Floorp not only shares its source code but also its build environment, inviting users to contribute and build their unique versions. The regular updates, based on Firefox ESR, ensure that you always have the latest features and security enhancements. Get to the point with Floorp Lightning's minimalism With a keen eye on user preferences, Floorp is gearing up to launch "Floorp Lightning," a streamlined and performance-focused browser, harkening back to the fundamentals of web browsing. This browser has undergone a meticulous transformation, shedding more than 80% of the features that characterized its predecessor. What remains are only the high-demand functionalities within the Floorp ecosystem. The result is a sleek, lean, and swift web browser optimized for maximum efficiency. In the ever-accelerating digital world, "Floorp Lightning" is poised to offer users a refreshingly nimble and responsive browsing experience, set to debut in beta mode this November. Floorp key features: Strong Tracking Protection: Floorp offers robust tracking protection, safeguarding users from malicious tracking and fingerprinting on the web. Flexible Layout: Customize Floorp's layout to your heart's content, including moving the tab bar, hiding the title bar, and more for a personalized browsing experience. Switchable Design: Choose from five distinct designs for the Floorp interface, and even switch between OS-specific designs for a unique look Regular Updates: Based on Firefox ESR, Floorp receives updates every four weeks, ensuring up-to-date security even before Firefox's releases. No User Tracking: Floorp prioritizes user privacy by abstaining from collecting personal information, tracking users, or selling user data, with no affiliations with advertising companies. Completely Open Source: The full source code for Floorp is open to the public, allowing transparency and enabling anyone to explore and build their own version. Dual Sidebar: Floorp features a versatile built-in sidebar for webpanels and browsing tools, making it perfect for multitasking and quick access to bookmarks, history, and websites. Flexible Toolbar & Tab Bar: Customize your browser with Tree Style Tabs, vertical tabs, and bookmark bar modifications, catering to both beginners and experts in customization. User-Centric Web Experience: Floorp prioritizes user privacy and collaboratively blocks harmful trackers. Floorp 11.29.0 changelog: Security fixes Download: Floorp 64-bit | 85.2 MB (Open Source) Links: Floorp Website | Github Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • "ethical", what an interesting term. Subjectively objective but objectively subjective. I argue there are very few "ethics" in copyright. Hell, I see very few applications of true ethics in anything. I see a great deal of unethical actions being done under the guise of ethics however. How unethical... I greatly prefer open source LLMs. Copyright ethics are mickey mouse deep.
    • How do you propose anyone govern who can use what online without completely destroying online privacy? Protip: You can't, so you shouldn't. I'm sorry, but I don't think of the children, that's their parents jobs. If their parents fail them, that's on them. Tough luck.
  • Recent Achievements

    • Collaborator
      fernan99 earned a badge
      Collaborator
    • Collaborator
      MikeK13 earned a badge
      Collaborator
    • One Month Later
      Alexander 001 earned a badge
      One Month Later
    • One Month Later
      Antonio Barboza earned a badge
      One Month Later
    • Week One Done
      Antonio Barboza earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      591
    2. 2
      ATLien_0
      225
    3. 3
      Michael Scrip
      167
    4. 4
      Xenon
      139
    5. 5
      +FloatingFatMan
      128
  • Tell a friend

    Love Neowin? Tell a friend!