user2617190
user2617190

Reputation: 41

Next and previous linking in JavaScript image gallery not working

I need to have multiple galleries on the same page (with captions), and have been trying to implement this javascript, but I cannot get the next | previous links to work for either gallery. New to javascript - any suggestions?

Here is the fiddle: http://jsfiddle.net/astHh/11/

HTML:

<div style="text-align: center">
  <!--  Place the first image here  -->
  <img src="http://www.cool-smileys.com/images/out11.jpg" id="mypic" name="mypic" alt="information" border="0" height="150" width="200">
  <br>
  <!--  Place the text for the first image here  --><div id="burns">Caption one</div>
  <p>
  <a href="#" onclick="S2.UpDown(-1); return false;">&laquo; Previous</a> |
  <a href="#" onclick="S2.UpDown(1); return false;"> Next &raquo;</a>
</div>

<p>

<div style="text-align: center">
  <!--  Place the first image here  -->
  <img src="http://www.cool-smileys.com/images/out13.jpg" id="mypic2" name="mypic2" alt="information" border="0" height="150" width="200">
  <br>
  <!--  Place the text for the first image here  --> <div id="burns2">Caption two</div>
  <p>
  <a href="#" onclick="S2.UpDown(-1); return false;">&laquo; Previous</a> |
  <a href="#" onclick="S2.UpDown(1); return false;"> Next &raquo;</a>
</div>

Javascript:

<script type="text/javascript">


function SimpleSlideShow(o){
 this.img=document.getElementById(o.ImageID);
 this.txt=document.getElementById(o.TextID);
 this.ary=o.Array||[];
 this.cnt=0;
}

SimpleSlideShow.prototype.UpDown=function(ud){
  this.cnt+=ud;
  this.cnt=this.cnt<0?this.ary.length-1:this.cnt==this.ary.length?0:this.cnt;
  if (this.ary[this.cnt]){
   if (this.img&&this.ary[this.cnt][0]){
    this.img.src=this.ary[this.cnt][0];
    this.img.alt=this.ary[this.cnt][1];
   }
   if (this.txt){
    this.txt.innerHTML=this.ary[this.cnt][2]||'';
   }
  }
 }

S1=new SimpleSlideShow({
 ImageID:'mypic',
 TextID:'burns',
 Array:[
  ['http://www.cool-smileys.com/images/out11.jpg,' 'Caption one', 'The beautiful mountains'],
  ['http://www.cool-smileys.com/images/out10.jpg','Caption two','The crystal clear lake'],

]
});

S2=new SimpleSlideShow({
 ImageID:'mypic2',
 TextID:'burns2',
 Array:[
  ['http://www.cool-smileys.com/images/out13.jpg','caption one', 'The beautiful mountains'],
  ['http://www.cool-smileys.com/images/out12.jpg','caption two','The lonesome, barren tree']
]
});


</script>

Upvotes: 0

Views: 914

Answers (1)

ArrayKnight
ArrayKnight

Reputation: 7356

You have multiple syntax errors in your javascript:

Remove from the script pane:

<script type="text/javascript">

</script>

Within your definition of "S1", you're missing a comma (just before 'Caption one'):

S1=new SimpleSlideShow({
    ImageID:'mypic',
    TextID:'burns',
    Array:[
        ['http://www.cool-smileys.com/images/out11.jpg,','Caption one', 'The beautiful mountains'],
        ['http://www.cool-smileys.com/images/out10.jpg','Caption two','The crystal clear lake'],
    ]
});

Then in the HTML pane, you should update the first set prev/next links to target S1 instead of S2.

Updated fiddle: http://jsfiddle.net/astHh/12/

Upvotes: 2

Related Questions