Wednesday, April 1, 2009

[jQuery] Using the title attribute if images as captions

Hi,

I'm trying to use the title attribute of images to create a simple
caption below the image. To do so, I've been trying to wrap the image
(floated left or right) in a div and add a caption that's taken from
the image attribute.

So far, I have:

Code:
// Create caption from title attribute for images with
class="imgcaption"
$j(function(){
var caption = $j('img').attr('title');
$j('img.cright').wrap('<div></div>');
$j('img.cright div').append(caption);
});

$j(function(){
var caption = $j('img').attr('title');
$j('img.cleft').wrap('<div></div>');
$j('img.cright div').append(caption);
});


But this isn't working. Can anyone see what the problem is? When I
change the variable to:

Code:
var caption = $j(this).attr('title');

It just uses the title attribute of the HTML document. Ideally, I'd
like to wrap the caption in a <span> tag as well, but don't know how
to do that.

Any ideas greatly appreciated.

Thanks

osu

No comments: