R
Roy Reed
I found a script that converts image alt text to a caption which I've tried
to modify for my needs:
function addCaption(imgElem) {
var captionElem = document.createElement("div");
captionElem.className = "caption";
var captionText = document.createTextNode(imgElem.alt);
captionElem.appendChild(captionText);
if(imgElem.nextSibling)
imgElem.parentNode.insertBefore(captionElem,imgElem.nextSibling);
else
imgElem.parentNode.appendChild(captionElem);
with(imgElem.style) {
captionElem.style.width =
(imgElem.width+borderLeftWidth+borderRightWidth+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="javascript: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
to modify for my needs:
function addCaption(imgElem) {
var captionElem = document.createElement("div");
captionElem.className = "caption";
var captionText = document.createTextNode(imgElem.alt);
captionElem.appendChild(captionText);
if(imgElem.nextSibling)
imgElem.parentNode.insertBefore(captionElem,imgElem.nextSibling);
else
imgElem.parentNode.appendChild(captionElem);
with(imgElem.style) {
captionElem.style.width =
(imgElem.width+borderLeftWidth+borderRightWidth+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="javascript: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