I found a script that converts image alt text to a caption which I've tried
to modify for my needs:
function addCaption(imgE lem) {
var captionElem = document.create Element("div");
captionElem.cla ssName = "caption";
var captionText = document.create TextNode(imgEle m.alt);
captionElem.app endChild(captio nText);
if(imgElem.next Sibling)
imgElem.parentN ode.insertBefor e(captionElem,i mgElem.nextSibl ing);
else
imgElem.parentN ode.appendChild (captionElem);
with(imgElem.st yle) {
captionElem.sty le.width =
(imgElem.width+ borderLeftWidth +borderRightWid th+paddingLeft+ paddingRight)+" px";
}
return true;
}
<img src="forest.jpg " width="515" height="183" alt="Strathyre Forest at
Dawn - the forest is bathed in golden light and mist rises from between the
trees - © Roy Reed, 1987" onload="javascr ipt:addCaption( this)">
This works as I want if no border or padding is added to the image, but not
if they are. In IE6 the padding is added to the width of the created div,
but not the border width. In Firefox nothing is added.
Can anyone tell me how this should be coded.
Live example (with attribution to original code) at
www.reeddesign.co.uk/autocaption/
Thanks
Roy Reed