Numerous slide bars within a single HTML document

As I continue learning how to code, I've encountered an issue with my website project. I'm trying to incorporate multiple image sliders, but only the first one is functioning properly. The images on the other sliders are not displaying. I suspect that I may be missing some essential code to make them work collectively, but I'm unsure of what exactly is needed. The provided code snippet shows how to create a slider, and I wonder how I can apply this code to generate multiple working sliders in a single HTML file. For instance, duplicating the slider and inserting it into the same file with different images while ensuring all sliders function as intended. Any assistance would be greatly appreciated.


            var slideIndex = 1;
            showSlides(slideIndex);

            function plusSlides(n){
                showSlides(slideIndex += n);
            }

            function currentSlide(n){
                showSlides(slideIndex = n);
            }

            function showSlides(n){
                var i;
                var slides = document.getElementsByClassName("mySlides");
                var dots = document.getElementsByClassName("dot");

                if (n > slides.length) {
                    slideIndex = 1;
                }
                
                if (n < 1) {
                    slideIndex = slides.length;
                }

                for (i = 0; i < slides.length; i++){
                    slides[i].style.display = "none";
                }

                for (i = 0; i < dots.length; i++){
                    dots[i].className = dots[i].className.replace(" active", "");
                }

                slides[slideIndex - 1].style.display = "block";
                dots[slideIndex - 1].className += " active";
            }
        

            .slideshowContainer{
                width: 43.2em;
                height: 23.15em;
                margin-top: 1.5em;
                margin-left: 1.5em;
                background-color: #FFF;
                box-shadow: -0.05em 0em 0.5em 0.05em rgba(0,0,0,0.4);
                /* Other CSS properties */
            }
            
            .mySlides{
                display: none;
            }

            /* Additional CSS code snippets here */
        

            <!-- HTML code for slideshow -->
            <!-- Duplicate this section to create additional sliders -->

            <div class="slideshowContainer">
                <!-- Slide content -->
                <!-- Include multiple 'mySlides' div elements with respective images -->
                <!-- Add navigation buttons and dots accordingly -->
            </div>
        

Answer №1

Simply duplicating your code will not be effective due to instances like this:

var slides = document.getElementsByClassName("mySlides");

This line of code will impact all sliders because they all contain elements with the same class name, mySlides.

A solution would involve changing all class names and IDs throughout the code (HTML, CSS, JS). However, it is highly recommended to first truly comprehend your code in order to enhance reusability.

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

What is the best way to display an income statement div with three elements in a vertical position once the screen reaches a medium width?

Can anyone help me create an income statement div with three elements that has a responsive layout? I want these elements to change from a horizontal display to a vertical one when there is limited space, especially on medium width screens. The initial la ...

Can you explain the significance of this %s value?

While going through some code, I found this: form method='post' input type='hidden' name='input' value='%s' I'm puzzled about the meaning of %s. I attempted to search online for an answer but couldn't fin ...

What is the best way to make a table row stand out when clicked on in a Vuejs application

How can I make a table row highlight when clicked in Vuejs using '@click'? I'm currently facing difficulties in achieving this. Below is my html template where I'm applying the class 'active' to the Boolean 'isActive&apo ...

ReactJS: Unable to navigate to a new page when moving from a page with automatic refresh

Every 5 seconds, I automatically refresh a page (Page1) using setTimeout and a callback function. However, every time I navigate to a new page (Page2), it quickly redirects back to Page1 after a few moments. I have tried using window beforeunload event l ...

Tips for Properly Positioning Floating Pop-Up Messages Using CSS and jQuery

I was experimenting with adding a button that triggers a popup message to my website. I followed a coding tutorial using jQuery and CSS, which looks like this: Javascript : !function (v) { v.fn.floatingWhatsApp = function (e) {...} }(jQuery); CSS : .fl ...

What is the best way to test out CSS effects before they go live

I enjoy customizing the CSS, like the posted dates on my portfolio How can I make these changes without affecting the public view of the website? One suggestion from a forum was to use .date {visibility:hidden;} ...

Is there a way to change the color of the menu button to white if the points are not visible?

I have created CSS to style the menu's color and make the buttons change color based on different actions. However, I also want the buttons to match the colors of the points on the map. To achieve this, I set the background color in JavaScript when ge ...

What is the reason for background images not loading when using `display: none`?

I'm really struggling to understand this puzzle: When the page loads, will mypic.jpg be downloaded by the browser? #test1 { display: none; } #test2 { background-image: url('http://www.dumpaday.com/wp-content/uploads/2017/01/random-pic ...

Issue with Bootstrap 5 Card Header not extending to full width

I have implemented tables within cards using a CDN. While the table looks fine in wide widths, it does not resize to fill the entire width when squeezed. Below is the HTML code: <div class="card table-responsive"> <div clas ...

Difficulty encountered while implementing Ajax POST in CodeIgniter

I've been working on a project similar to a ticket system that occasionally requires lengthy answers. When using CKEDITOR in the answer area, the agent's changes are automatically saved to the database using Json GET. However, I encountered an er ...

Is there a way to interact with a Bootstrap 5 dropdown in React without triggering it to close upon clicking?

I'm currently working on creating a slightly complex navigation bar using Bootstrap 5 and ReactJS. The issue I'm encountering involves the dropdown menu within the nav bar. Whenever I click inside the dropdown, even if it's just non-link te ...

Focus on an empty <input> tag with just the type attribute

In what way can React Testing Library be utilized to isolate a blank <input> element that solely possesses a type attribute? For instance, consider an input field that will eventually have attributes added dynamically, much like the surrounding labe ...

Chrome extensions using Cross-Origin XMLHttpRequest

Chrome extensions API states that cross-origin calls using the XMLHttpRequest object should be allowed with proper permissions: An extension can communicate with remote servers beyond its origin by requesting cross-origin permissions. Following the Goog ...

Having trouble figuring out how to play an mp3 using the <audio> tag. The console is showing an error message that says, "Unable to decode media resource XYZ."

I've been frustrated while trying to make the HTML5 <audio> element function properly. My webpage has two elements - one serving a file from my server and the other serving the same file from a different source server: <audio preload="metada ...

Navigating through directory paths in JavaScript can be a daunting task for many

In my app.js file, I've included the following code: app.use(multer({dest:'./uploads'})) What does './uploads' refer to here? It is located in the same directory as app.js. In what way does it differ from simply using uploads? I ...

The system is unable to locate the command "nvm"

Lately, I've been experimenting with using nvm to manage different versions of node. After successfully installing nvm on my Mac OS Catalina(10.15.6), I was able to easily switch between versions through the terminal. However, when attempting to do t ...

In what way does AngularJS asynchronously navigate through the Angular template made up of HTML and AngularJS Directives?

While diving into the AngularJS book penned by Brad Green and Shyama Sheshadari, I stumbled upon the following insightful passage: The initial startup sequence unfolds as follows: 1. A user triggers a request for your application's first page. ...

Utilizing JSON File as an Array in a Node.JS Environment

I'm struggling with converting a .json file into an array object using NodeJS, Here's the JSON content: { "cat": { "nani": "meow" }, "dog": { "nani": "woof" } } index.js: const array = require('../../data/use ...

Combine TypeScript files in a specific sequence following compilation

I am hoping to utilize gulp for the following tasks: Compiling TypeScript to JavaScript, which is easily achievable Concatenating JavaScript files in a specific order, proving to be challenging Since I am developing an Angular application, it is crucial ...

Phonegap - Retaining text data in a checklist app beyond app shutdown

This is my first time developing an app with Phonegap. I am looking to create a checklist feature where users can input items into an input field. However, I am struggling with figuring out how to save these items so that they remain in the checklist even ...