...

View Full Version : jquery: creating image caption from alt



lauthiamkok
08-15-2009, 06:04 PM
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,


$("#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

Spudhead
08-17-2009, 05:49 PM
$("#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.

lauthiamkok
08-17-2009, 06:11 PM
$("#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 :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum