Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-15-2009, 05:04 PM   PM User | #1
lauthiamkok
Regular Coder

 
Join Date: Dec 2008
Posts: 117
Thanks: 14
Thanked 0 Times in 0 Posts
lauthiamkok is an unknown quantity at this point
jquery: creating image caption from alt

hi,
I am trying to create image caption by getting the value from alt in each img tag. i manage to get the alt value from each img tag with each();.

and i use insertAfter() to insert the alt value after each image, but it turns out very strange, have a look from the link below,

http://lauthiamkok.net/tmp/jquery/jquery.image_caption/

it create a list of captions instead!

what have done wrong to make this image caption function?

this is the code i made so far,

Code:
$("#content img").each(function () {
	var title = $(this).attr("alt");
	$(document.body).append("<div class='caption'>"+ title +"</div>");	
	$(".caption").insertAfter("#content img");
	});
});
many thanks if u have any idea.

thanks,
Lau
lauthiamkok is offline   Reply With Quote
Old 08-17-2009, 04:49 PM   PM User | #2
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
Code:
$("#content img").each(function () {
	var $this = $(this);
	var title = $this.attr("alt");
	$this.after('<div class="caption">'+ title +'</div>');
});
Untested, but that should do it.
Spudhead is offline   Reply With Quote
Old 08-17-2009, 05:11 PM   PM User | #3
lauthiamkok
Regular Coder

 
Join Date: Dec 2008
Posts: 117
Thanks: 14
Thanked 0 Times in 0 Posts
lauthiamkok is an unknown quantity at this point
Quote:
Originally Posted by Spudhead View Post
Code:
$("#content img").each(function () {
	var $this = $(this);
	var title = $this.attr("alt");
	$this.after('<div class="caption">'+ title +'</div>');
});
Untested, but that should do it.
just tested - it works perfectly. thank you very much
lauthiamkok is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:59 AM.


Advertisement
Log in to turn off these ads.