Connecting Tech Pros Worldwide Help | Site Map

Adding a text caption to this slideshow script

Newbie
 
Join Date: Apr 2007
Posts: 21
#1: Apr 21 '07
Hi guys

I've been using this, my favorite slideshow script for its simplicity. Now I'd like to add a text caption to each image that rotates in-kind, but I'm having trouble working out the dynamics.
Here's the current script:

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--

myPix = new Array(
"images/picture01.jpg",
"images/picture02.jpg",
"images/picture03.jpg",
"images/picture04.jpg")
thisPic = 0
imgCt = myPix.length - 1

function chgSlide(direction) {
if (document.images) {
thisPic = thisPic + direction
if (thisPic > imgCt) {
thisPic = 0
}
if (thisPic < 0) {
thisPic = imgCt
}
document.myPicture.src=myPix[thisPic]
}
}

// -->
</SCRIPT>

<P><IMG SRC="images/picture01.jpg" NAME="myPicture" WIDTH="300" HEIGHT="200"></P>
<A HREF="javascript:chgSlide(-1)"><< Back</A> <A HREF="javascript:chgSlide(1)">Forward >></A>

I was thinking that I could add this second array, but am stuck on how to integrate the resulting variable into the body:

myCap = new Array(
"Text Caption 01",
"Text Caption 02",
"Text Caption 03",
"Text Caption 04")
thisCap = 0
imgCt = myCap.length - 1

function chgSlide(direction) {
if (document.images) {
thisCap = thisCap + direction
if (thisCap > imgCt) {
thisCap = 0
}
if (thisCap < 0) {
thisCap = imgCt
}
document.myCaption.src=myCap[thisCap]
}
}

Any ideas?
Newbie
 
Join Date: Apr 2007
Posts: 21
#2: Apr 21 '07

re: Adding a text caption to this slideshow script


I found a simple way to use a textarea tag, but it looks junky.

<TEXTAREA NAME=myCaption></TEXTAREA>

I'd like for the text to appear as if it was simple HTML text.
Newbie
 
Join Date: Apr 2007
Posts: 21
#3: May 19 '07

re: Adding a text caption to this slideshow script


I tried combining the variables to no avail, but I think I'm getting closer. This variant tells me, however, that 'myCaption' is null, even though I follow the other array's scripting precisely. What am I doing wrong?

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--

myPix = new Array(
"images/01.jpg",
"images/02.jpg",
"images/03.jpg")
thisPic = 0
imgCt = myPix.length - 1

myTxt = new Array(
"Caption 1",
"Caption 2",
"Caption 3")
thisTxt = 0
txtCt = myTxt.length - 1

function chgSlide(direction) {
if (document.images) {
thisPic = thisPic + direction
thisTxt = thisTxt + direction
if (thisPic > imgCt) {
thisPic = 0
thisTxt = 0
}
if (thisPic < 0) {
thisPic = imgCt
thisTxt = txtCt
}
document.myPicture.src=myPix[thisPic]
document.myCaption.src=myTxt[thisTxt]
}
}

// -->
</SCRIPT>


<P><IMG SRC="images/01.jpg" NAME="myPicture" WIDTH="50" HEIGHT="40" ALT="Slideshow"></P>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
document.write(myCaption)
</SCRIPT><BR>
<A HREF="javascript:chgSlide(-1)">Back</A> <A HREF="javascript:chgSlide(1)">Forward</A>
Familiar Sight
 
Join Date: Feb 2007
Posts: 207
#4: May 20 '07

re: Adding a text caption to this slideshow script


Quote:

Originally Posted by Wolfman

document.myCaption.src=myTxt[thisTxt]

You don't have an element called document.myCaption so you cant give it a src property, and even if you could it wouldn't be any use.

Create a <span> element with whatever CSS styling you want, give it an ID attribute, say 'myCaption', include the function below and use it thus:

caption('myCaption', myTxt[thisTxt]);

Expand|Select|Wrap|Line Numbers
  1. function caption(elemId, txt)
  2. {
  3.  var elem;
  4.  
  5.  if(document.getElementById && (elem=document.getElementById(elemId)))
  6.   if(elem.firstChild)
  7.    elem.firstChild.data=txt;
  8.   else
  9.    elem.appendChild(document.createTextNode(txt)); 
  10. }
@ADMIN - Why are we now getting those poxy line numbers that nobody wants?

Alternatively, try a different slideshow
Newbie
 
Join Date: Apr 2007
Posts: 21
#5: May 20 '07

re: Adding a text caption to this slideshow script


Thanks. I'm motivated to use this one because it's very basic - no transition effects, no page refreshing, no fancy text boxes. Just a picture and some text that can be formatted using traditional tags. Will give your corrections a whirl - and thanks.
Reply


Similar JavaScript / Ajax / DHTML bytes