Using image title attribute for caption

Using image title attribute for caption

Hi,

I've been trying and trying to figure this one out, but can't seem to jump the last hurdle. I'm sure it's pretty simple, so maybe someone here can help.

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 (given a class of cleft or cright depending on whether it's floated left or right) in a div and a caption that's taken from the image attribute.

So far, I have:

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


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

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