Trouble with the navigation of the JavaScript image gallery: next and previous buttons are not

Is it possible to have multiple galleries on a single page with captions? I've been trying to incorporate this JavaScript code, but struggling to make the next/previous links function for both galleries. Since I'm new to JavaScript, any advice or suggestions would be greatly appreciated.

If you'd like to take a look at the code, here's the fiddle: http://jsfiddle.net/astHh/11/

HTML:

<div style="text-align: center">
  <img src="http://www.cool-smileys.com/images/out11.jpg" id="mypic" name="mypic" alt="information" border="0" height="150" width="200">
  <br>
  <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">
  <img src="http://www.cool-smileys.com/images/out13.jpg" id="mypic2" name="mypic2" alt="information" border="0" height="150" width="200">
  <br>
  <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>

Answer №1

Your JavaScript code contains several syntax errors that need to be addressed:

Firstly, you should remove the following lines from the script section:

<script type="text/javascript>

</script>

In your definition of "S1", make sure to add 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'],
    ]
});

Lastly, update the links in the HTML section to target S1 instead of S2 for the prev/next functionality.

Here is the updated fiddle link: http://jsfiddle.net/astHh/12/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble with displaying the NVD3 sample chart

I seem to be missing something simple... I've copied the code for an nvd3 example exactly, but I'm getting a blank page without any error messages. Both the nvd3 and d3 libraries are showing up when I check in the console by typing "nv" or "d3", ...

Error: jQuery function xxxx is not defined

Upon initial launch of the mobile app homepage, an error is encountered. The error message "TypeError: Jqueryxxxxxx is not a function" is displayed, although the API callback results are shown as "jQuery111309512500500950475_1459208158307({"code":1," ...

Unable to retrieve custom date picker value with React Antd

I have implemented a custom datepicker for entering dates in the 'MMDD' or 'MMDDYY' format. The date value is stored in state and used in the datepicker component as a controlled component. However, upon form submission, the datepicker ...

vaadin-grid selection issue not functioning

I'm encountering an issue with the row selection feature. The selectedItems array only updates when I select all items at once. I'm not sure if I'm missing something or if this is a bug. selectedItems: An array that contains the selected ...

What is the best way to incorporate a <li> view cap within a div element using JavaScript?

Currently, I am attempting to implement a dynamic <li> view limit within the content of a div. My goal is to display only 3 <li> elements each time the div content is scrolled. Although not confirmed, I believe this example may be helpful: ...

I'm only appending the final element to the JavaScript array

Currently, I have the following code: I'm endeavoring to create a new JSON object named dataJSON by utilizing properties from the GAJSON object. However, my issue arises when attempting to iterate over the GAJSOn object; only its last element is added ...

Tips for accessing the value of a dynamically created textbox using JavaScript

Hello everyone, I have a couple of questions that I need help with. I am currently working on developing a social networking website similar to Facebook. On this platform, there are multiple posts fetched from a database. However, I am facing an issue w ...

Fade In/Out Scroll Effect

As the user scrolls down the page, I have a slider that smoothly slides from right to left. What I'm trying to achieve is for the slider to fade out when the last slide is no longer in view during downward scrolling, and fade back in as the last slid ...

What is the purpose of using an open quote and bracket in the `eval('('+jsonString+')')` syntax for parsing a JSON string?

What is the rationale behind this particular syntax structure? eval('(' + jsonString+ ')') When it comes to parsing JSON text, Crockford explains that "The text must be wrapped in parentheses to prevent any confusion with JavaScript& ...

How can you center the body of a webpage while using the zoom in and zoom out features in HTML5?

What is the best way to maintain body alignment while zooming in and out on a web page? <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> body { font-family: 'Merriweather Sans&apos ...

Despite using Vue and Vuex with Axios asynchronously, the getters are still returning an empty array

I am encountering an issue with getters that are returning the initial state (an empty array). In my component, I have a method called created that sets the axios call result into the state. created() {this.$store.dispatch("SET_STORIES");}, I am using m ...

Enhancing the shadow effects on ThreeJS using buffergeometry

I'm currently working on a project where I have loaded a .gltf model in ThreeJS with a keyframe animation that alters the shape of the object (it's a point level animation). Everything seems to be functioning correctly, except for the fact that t ...

Create a "load additional data" button

I'm working on building my blog and I've run into a roadblock while trying to implement a load more button. Here's what I currently have: actions: { LOAD_ARTICLE_LIST: function ({ commit }) { axios.get('someurl/articles') ...

Issues with the functionality of the jQuery UI datepicker arise following a body replacement

Currently, I am working on a project that involves updating the page content via ajax by replacing the entire body. However, I have encountered an issue with using the jQuery datepicker in this scenario. The datepicker functionality works fine until the co ...

How to exclude the port number from the href in a Node.js EJS template?

I have the following code snippet. I am trying to list out the file names in a specific directory and add an href tag to them. The code seems to be working fine, however, it still includes the port number where my node.js app is running. How can I remove ...

What is the best way to navigate to the bottom of a page when new data is added?

I've created a chat feature in my Ionic app, but the issue is that when a user receives a new message while being on the chat screen, the view doesn't automatically scroll down to show the new message. The user has to manually scroll down to see ...

What is the best way to enlarge text size with jquery?

I am attempting to: $('a[data-text="size-bigger"]').click(function () { a=$('span'); b=$('span').css('font-size'); a.css('font-size', b+1); } ); Initially, I ha ...

"Troubleshooting Image Loading Issues in Next.js: A Guide to Reloading Unresponsive

Hello, I am facing an issue with rendering 300 images from IPFS data. Occasionally, around 3-4 of the images fail to load and result in a 404 error. Even after refreshing the page, these unloaded images remain inaccessible. It seems like they are permanent ...

Repairing a syntax error in a jQuery selector variable

$(".className").click(function(){ var link = $(this).find("a").attr('href'); //output is '#myID' var findItems = $(link '.mydiv').length; //WRONG var findItems = $(link + '.mydiv').length; ...

Attempting to access a shared php/javascript library using mod_rewrite

Let's dive into a fresh perspective on a question I previously raised: I've crafted a mod_rewrite snippet that checks for the existence of JavaScript, CSS, and PHP files on the subdomain they are called from (e.g., subdomain.example.com). If the ...