Forum Moderators: open

Message Too Old, No Replies

Change document text

Change image caption in slideshow

         

adb64

11:09 am on Nov 3, 2005 (gmt 0)

10+ Year Member



Hello all,

For a slideshow I want to change the image caption text when a new image is displayed. Until now I could not find a way to change a part of the text in the document. Below you'll find my testcode:


<html>
<head>
<title>Test</title>
<script language="javascript" type="text/javascript">
<!--
var Images;
var NrImages;
var CurImage;

function Preload()
{
Images = new Array();
NrImages = 5;
CurImage = 0;

Images[0] = new Image();
Images[0].src = "Img1.jpg";
Images[0].alt = "Image 1 Caption";
Images[1] = new Image();
Images[1].src = "Img2.jpg";
Images[1].alt = "Image 2 Caption";
Images[2] = new Image();
Images[2].src = "Img3.jpg";
Images[2].alt = "Image 3 Caption";
Images[3] = new Image();
Images[3].src = "Img4.jpg";
Images[3].alt = "Image 4 Caption";
Images[4] = new Image();
Images[4].src = "Img5.jpg";
Images[4].alt = "Image 5 Caption";

ShowImage(CurImage);
}

function Previous()
{
if (CurImage == 0)
{
CurImage = NrImages;
}
CurImage--;
ShowImage(CurImage);
}

function Next()
{
CurImage++;
if (CurImage >= NrImages)
{
CurImage = 0;
}
ShowImage(CurImage);
}

function ShowImage(Nr)
{
image.src=Images[Nr].src;
image.alt=Images[Nr].alt;
caption.value=Images[Nr].alt;
}
-->
</script>
</head>
<body onload="Preload();">
<img id="image" src="Img1.jpg" alt="">
<br>
<div id="caption">Image Caption</div>
<br>
<a href="#" onclick="Previous(); return false;">Previous</a>
&nbsp;&nbsp;&nbsp;
<a href="#" onclick="Next(); return false;">Next</a>
</body>
</html>

With this code I want to change the text within the div with id 'caption'. I know my implementation above doesn't work. I managed to use a button and change the text in the button, but I would prefer to change the text in the document.

Has anybody an x-browser solution for this.

Thanks
Arjan

adb64

12:14 pm on Nov 3, 2005 (gmt 0)

10+ Year Member



Found something that works, well at least at IE6. That's the only browser I have at hand at this moment.
Instead of

caption.value=Images[Nr].alt;

in the function ShowImage(). I now use:

caption.innerHTML=Images[Nr].alt;

But I wonder what the x-browser support is for this. Does anybody have an idea about the support for innerHTML?

Thanks,
Arjan

kaled

12:41 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think this should explain where you are going wrong.

var capn=document.getElementById('caption');
if (capn) capn.innerHTML='yippee';

Kaled.