473,385 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Convert ALT to Caption

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,imgEle m.nextSibling);
else
imgElem.parentNode.appendChild(captionElem);
with(imgElem.style) {
captionElem.style.width =
(imgElem.width+borderLeftWidth+borderRightWidth+pa ddingLeft+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 - &copy; 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
Aug 14 '06 #1
3 1458

Roy Reed написав:
with(imgElem.style) {
captionElem.style.width =
(imgElem.width+borderLeftWidth+borderRightWidth+pa ddingLeft+paddingRight)+"px";
}
You add string values.

<img width="500px"style="padding: 5 5 5 5">
In this case instead of "510px" you will get "500px5px5pxpx"
Use parseInt or parseFloat before summation.

Aug 15 '06 #2

"marss" <ma***@ukr.netwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...

Roy Reed ???????:
> with(imgElem.style) {
captionElem.style.width =
(imgElem.width+borderLeftWidth+borderRightWidth+p addingLeft+paddingRight)+"px";
}
>You add string values.

<img width="500px"style="padding: 5 5 5 5">
In this case instead of "510px" you will get "500px5px5pxpx"
Use parseInt or parseFloat before summation.
Is this what you meant?

with(imgElem.style) {
captionElem.style.width = parseFloat(imgElem.width+borderLeftWidth+
borderRightWidth+paddingLeft+paddingRight)+"px";
}

It gives exactly the same result.

Thanks for trying to help with this. I'm not a JavaScript expert.

Roy
Aug 15 '06 #3

Roy Reed написав:
Is this what you meant?

with(imgElem.style) {
captionElem.style.width = parseFloat(imgElem.width+borderLeftWidth+
borderRightWidth+paddingLeft+paddingRight)+"px";
}
No. I mean something like this
with(imgElem.style) {
captionElem.style.width =
(parseFloat(imgElem.width)+parseFloat(borderLeftWi dth)+

parseFloat(borderRightWidth)+parseFloat(paddingLef t)+parseFloat(paddingRight))+"px";
}

Aug 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Wally | last post by:
I would like to have an image with a caption displayed below it. The size of the image will vary. The caption should not extend beyond the width of the image. How can I cause the text of the...
2
by: Phil Stanton | last post by:
When designing a new form or report, the Default ForeColor is often something like -2147483640 which is the colour of Windows text (possibly black) and the default backColor is -2147483643...
1
by: altacct | last post by:
I've got a multi-page subform with a tab control on one of the pages. The code that moves you from page three to page four, which is where the tab is, changes the tab captions according to some...
2
by: Mel WEaver | last post by:
Hello, I have the following delphi structure for a binary file. I'm looking for idea how to read this file. Mel type TMenuDataStruct = packed record exename : string;
0
by: Chenghui Li | last post by:
We have a problem with the Windows XP theme: We have a IDE which allows other developers to develop visual programs for their customers. Our IDE allow them to set font for window captions easyly...
11
by: John Ortt | last post by:
Hi everyone. I have a database which I have developed in Access 2000 which is working nicely. The problem is that my customer only has Access 97. I tried to convert the database but the main...
6
by: Bob Darlington | last post by:
I want to use the caption property for fields in a recordset as a condition in a loop. That is, I only want to consider those fields which have captions: For each fld in RecordsetName.Fields If...
9
by: Neal | last post by:
http://www.brassattackofspringfield.com/gigs.html CSS at http://www.brassattackofspringfield.com/default.css Opera and IE render caption as desired. Firefox does not. Why? And is there a fix?
7
by: jimmy1 | last post by:
Hello everyone I am trying to find a way to convert seconds to minutes in the script below - Private Sub cmdBegin_Click() On Error Resume Next n = Val(txttime.Text) * 60 tmrClock.Enabled = True...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.