Multiple presentations displayed on a single webpage

I have searched through various threads with similar questions, but I cannot seem to find information that is relevant to the functions I am working with.

My issue lies with creating an automatic slideshow using HTML, CSS, and JS. While it works perfectly for a single slideshow, I am unable to get it to work for two slideshows simultaneously.

The JavaScript code I have tried includes different approaches, including creating separate functions for each slideshow. Unfortunately, none of these attempts have been successful.

// Necessary variables 
var images = [];
var i = 0;
var time = 2000;

// List of images
images[0] = "images/nighttime.png";
images[1] = "images/natur.png";
images[2] = "images/shark_1.png";
images[3] = "images/shark_2.jpg";

function autoSlide() {
  document.slide.src = images[i];

  if (i < images.length - 1) {
    i++;
  } else {
    i = 0;
  }

  setTimeout("autoSlide()", time);
}

window.onload = autoSlide;

/* Slideshow 2 */
// Necessary variables
var images_one = [];
var j = 0;

// List of images
images_one[0] = "images/innredning_1.png";
images_one[1] = "images/innredning_2.png";
images_one[2] = "images/innredning_bad.jpg";

function autoSlide_box2() {
  document.slide.src = images_one[j];

  if (j < images_one.length - 1) {
    j++;
  } else {
    j = 0;
  }

  setTimeout("autoSlide()_box2", time);
}

window.onload = autoSlide_box2;

In terms of the HTML structure, the overlay__content element is specifically designed for the openNav() function and should not interact with the slideshow functionality.

<section class="index-content">
        <!-- This container displays the images which change automatically -->
<div class="container">
    <figure id="slideshow" onclick="openNav()">
        <img src="images/nighttime.png" name="slide" class="pictures">
    </figure>
</div>

<!-- This div and its contents are invisible by default and appear when an image is clicked -->
<div id="nav" class="overlay">



<div class="overlay__content1">
    <a href="#" class="closeButton" id="closeButton" onclick="closeNav()" style="display:none;"> &times; </a>
    <a href="#" class="arrow" id="moveRight" onclick="plusSlides(1)" style="display:none;"></a>
    <a href="#" class="arrow" id="moveLeft" onclick="plusSlides(-1)" style="display:none;"></a>

    <div class="mySlides fade">

        <img src="images/nighttime.png">

    </div>

    <div class="mySlides fade">

        <img src="images/natur.png">

    </div>

    <div class="mySlides fade">

        <img src="images/shark_1.png">

    </div>

    <div class="mySlides fade" id="threenfour">

            <img src="images/shark_2.jpg">

    </div>

</div>
</div>
</section>


<section class="index-content2">
    <div class="containerLeft">
        <figure id="slideshow" onclick="openNav()">
            <img src="images/innredning_1.png" name="slide" class="pictures">
        </figure>
    </div>

    <!-- This div and its contents are invisible by default and appear when an image is clicked -->
    <div id="nav" class="overlay">



    <div class="overlay__content2">


        <div class="mySlides fade">

            <img src="images/innredning_1.png">

        </div>

        <div class="mySlides fade">

            <img src="images/innredning_2.png">

        </div>

        <div class="mySlides fade">

            <img src="images/innredning_bad.jpgw">

        </div>


    </div>
    </div>
</section>

Answer №1

I'm not entirely certain, but it seems like you may be referring to the same slide in your functions:

document.slide.src = images[i];

and

document.slide.src = images_one[i];

You might want to consider assigning IDs to your images, as suggested in this response by NewToJS

Answer №2

I managed to find the solution by adjusting the function to update the images in all slideshows on the webpage.

function autoSlide () {

  // Updating the images for each slideshow
  document.slide.src = images[i];
  document.slide1.src = images_one[j];
  document.slide2.src = images_two[k];

  // Reset variables when reaching end of array
  if (i < images.length-1) {i++;}
  else {i = 0;}
  if (j < images_one.length-1) {j++;}
  else {j = 0;}
  if (k < images_two.length-1) {k++;}
  else {k = 0;}

  setTimeout("autoSlide()", time);
}

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

Update the URL for the source of the Server-Sent event

Is there a way to modify the source set in the declaration of EventSource? I attempted this approach: var source = new EventSource("/blahblah.php?path=" + (window.location.pathname)); // A few lines below... source.url = "/blahblah.php?path=" + url; Nev ...

Unexpected outcomes arising from using nested arrays with jQuery validation plugin

I have been utilizing the jQuery validation plugin from . I am encountering an issue with validating a nested array "tax_percents[]" that is within another array. The validation seems to only work for the first value in the array, and even for the second f ...

Is there a way to create a diagonal movement for an image by clicking on another HTML element?

<!DOCTYPE html> <html> <head> <meta charset="UTF-9"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(function() { $(document).re ...

JavaScript sticky navigation bar - error displayed in console

Hey there, I'm having an issue with my Sticky-menu. I've been trying to troubleshoot it but keep getting these console error messages: 1. Cannot read property 'offsetTop' of null at HTMLDocument. 2. Cannot read property 'class ...

What is the method for creating an array from an object?

I am facing a challenge with an object that has keys containing an index after the underscore. I need to gather each pair of keys with the same index together in order to achieve my expected object. Can someone help me figure out how to accomplish this u ...

Can JavaScript/jQuery be integrated with MySQL/PHP?

I have a unique question that I've been unable to find an answer to, which may indicate its complexity. The reason for my inquiry is because I am in the process of developing a food delivery website. Within the admin panel of this website, administrat ...

"Converting the initial row of a CSV file into the header section of

I have been trying to create a table using ajax but I am facing difficulty in enclosing the <th> elements inside the <tr id="header">. I have tried using methods like appendTo and prependTo but it hasn't worked for me. The desired structu ...

Chart showing a Google Timeline: Allow for each bar to be colored differently when there are multiple bars on the same line

It appears that the Google Timeline charts have a feature to color individual blocks on the timeline as indicated in the documentation at However, there seems to be an issue when two bars overlap on the same line, which is evident in this fiddle: http://j ...

Function for jQuery Slide Toggle is not being invoked

I'm struggling with jQuery Slide Toggle. $(document).ready(function() { $("a").click(function() { $(this).parent().parent().children("p").Toggle(400); return false; }); }); I have multiple posts displayed on the page fetched from ...

Managing Import Structure in Turborepo/Typescript Package

I am currently working on creating a range of TypeScript packages as part of a Turborepo project. Here is an example of how the import structure for these packages looks like: import { Test } from "package-name" import { Test } from "package ...

Is it possible to modify the color of a division row using an onchange event in JQuery?

I am facing a requirement to dynamically change the color of rows based on the dropdown onchange value. The rows are structured in divisions, which were previously tables. There are two main divisions with rows that need to be updated based on dropdown sel ...

Updating Angular model remotely without relying solely on the controller

I am struggling to call the addRectangleMethod method from my Javascript code in order to retrieve server-side information into the Angular datamodel. However, I keep encountering an error stating that the method I'm trying to call is undefined. It&ap ...

Effortless login authentication using Disqus through Google or Facebook tokens

I have set up my website to allow users to authenticate through Google and Facebook using passport, which uses the OAuth 2.0 API. Additionally, I am utilizing Disqus to manage the comments system on my site. However, I am running into a problem where user ...

Leveraging functions in a Node.js module

I am struggling with using a function inside a Node.js module. I have implemented a custom sorting function to sort an array of objects based on the value of a specific property. exports.getResult = function(cards) { cards.sort(sortByField('suit& ...

External javascript file not providing accurate data for Google Analytics tracking

Our Google Analytics account shows that we are receiving data. I uploaded to our Alpha site here on Monday and have only seen 1 visit on the 19th so far, with no activity for the rest of the week. Despite having visitors from other offices and states, the ...

Using the Ajax method from a separate class in TypeScript: A step-by-step guide

Recently, I started learning about typescript and ajax. One of the challenges I encountered was while creating a method in typescript for making ajax calls that can be used across classes: myFunc(value: string): JQueryPromise<any> { var dfd = $. ...

What is the best way to incorporate multiple Bootstrap templates into an Angular project without causing conflicts between them?

When incorporating a bootstrap template into the admin interface, it is important to consider that its design may vary from the template used for visitors. Mixing multiple styles based on different templates can lead to conflicting styles and can potenti ...

Begin by generating lines and text that originate from the center of spherical objects in ThreeJS

Currently, I am constructing a solar system simulation using ThreeJS. As part of the functionality, when I select a planet's name, the camera zooms in on that specific planet. My goal now is to create a visual element that consists of a 2D line origin ...

The response header with jQuery and AJAX

When attempting to retrieve response headers with JQuery and Ajax, I am only able to access a limited number of headers. Is there a way to access all of them? $(document).ready(function () { $.ajax({ type: 'POST', url:'http://do. ...

Changing classes within Twitter Bootstrap 3 causes a shift in style and layout

Currently, I am working with Twitter Bootstrap 3 and have a DIV that contains either an image or video. The div has a col-sm-3 class, but when hovering over it, I use jQuery to switch it to a col-sm-12. I am wondering if there is a way to create a smooth ...