Issue with Swiper JS: The buttons on pages three and beyond are unresponsive

I'm having trouble with the functionality of the "Back" button on pages 2 and 3 of my website. Can anyone help me figure out why it's not working?

My goal is to create a website that resembles a game menu, similar to Deus Ex The Fall. I'm using Swiper JS to create a slider with parallax effects for the background images. However, something seems to have gone wrong.

const swiper = new Swiper('.swiper', {
  // Optional parameters
  direction: 'horizontal',
  
  parallax: true,
  

});

document.querySelector('.back').onclick = () => {
  swiper.slideTo(0)
}

document.querySelector('.page1').onclick = () => {
  swiper.slideTo(1)
}

document.querySelector('.page2').onclick = () => {
  swiper.slideTo(2)
}

document.querySelector('.page3').onclick = () => {
  swiper.slideTo(3)
}
...
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Testttt</title>
    <link rel="stylesheet" href="./swiperjs/swiper-bundle.min.css">
    <link rel="stylesheet" href="./index.css">
    ...
<body>
            <div class="swiper">  
              <div class="swiper-wrapper">  
                <div class="parallax-bg" style="background-image:url(./images/panorama.jpg)" 
                data-swiper-parallax="100%"></div>
                    <!-- Slides --> 
                    <div class="swiper-slide">
                        <h1>Page 0</h1>
                      <div class="buttons">
                        <button class="page1">Page1</button>
                        <button class="page2">Page2</button>
                        <button class="page3">Page3</button>
                      </div>
                    </div>

The "Back" button works correctly on Page 1

                    <div class="swiper-slide">
                        <h1>Page 1</h1>
                      <div class="buttons">
                        <button class="back">Back</button>
                      </div>
                    </div>

However, the buttons are no longer functioning from this point onwards

                    <div class="swiper-slide">
                        <h1>Page 2</h1>
                      <div class="buttons">
                        <button class="back">Back</button>
                      </div>
                    </div>
                    <div class="swiper-slide">
                        <h1>Page 3</h1>
                      <div class="buttons">
                        <button class="back">Back</button>
                      </div>
                    </div>  
            </div>
        </div>

    <script src="./swiperjs/swiper-bundle.min.js"></script>
    <script src="./index.js"></script>
</body>
</html>

Answer №1

Instead of using querySelector to set the event listener, try using querySelectorAll which will give you all elements with that selector:

const els = document.querySelectorAll('.back');
els.forEach ( el => {
  el.onclick = () => {
    swiper.slideTo(0)
}
});

You may need to adjust the parameter passed in slideTo accordingly. You can also consider using a for loop instead of forEach.

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

Unwrapping Promises in Angular for Seamless Resolution

I recently started working with Angular and found myself in a large project. I encountered a simplified version of my code below: var beforeClose = function() { var closeDeferred = $q.defer(), a = $q.defer(), b = $q.defer(), c = $q.defer() ...

Is there a way to refresh my order hook in React JS?

I've been attempting to modify the values within my order hook, but for some reason, they aren't updating. I'm relatively new to React and can't figure out what mistake I might be making. Any insights? Take a look at my code snippet: ...

Element UI: Triggering an event when the sort caret is clicked

Is it possible to trigger an event when the sorting carets are clicked on a table with sortable columns, ideally with the same parameters as the header-click event? I am able to emit an event by clicking on the header of any sortable column (header-click) ...

Tips for modifying date format in Angular 8

My datepicker for a date column is displaying the incorrect date format after submission. I am looking to change this format to the correct one. I am working with bsConfig bootstrap in Angular 8, but I am unsure of how to modify the date format. The back ...

Tips for Choosing a Tab in angular-ui: AngularJS

Is there a way to select the last tab without using ng-repeat? I want to avoid using ng-repeat but still need to select tabs. Any suggestions? If you'd like to see the code in action, you can visit: http://plnkr.co/edit/ZJNaAVDBrbr1JjooVMFj?p=preview ...

When attempting to incorporate Jasmine into my current AngularJS/Bootstrap project, I encountered the error message stating

Currently, I am working on a group project that requires test coverage. The project utilizes npm and angularJS. To kick things off, I performed an npm install for jquery, jasmine, and angular-mocks. Since I'm relatively new to testing, I decided to st ...

Align text to the left of a button with Bootstrap 4

Currently, I have two buttons stacked on top of each other. I'm attempting to include some text to the left of the bottom button, but I am encountering two obstacles: The first issue is that it creates a white round-ish shape around my top button w ...

Retrieve the decimal separator and other locale details from the $locale service

After reviewing the angular $locale documentation, I noticed that it only provides an id (in the form of languageId-countryId). It would be helpful to have access to more specific information such as the decimal separator character. Is there a way to retri ...

GATSBY: Error: Unable to find the specified property 'includes' within an undefined value

I'm struggling to figure out how to properly filter images in my portfolio website as discussed in this post… Every time I attempt it, I encounter the following error: "TypeError: Cannot read property 'includes' of undefined" D ...

Is there a way to align both JSX elements in a single row without having one overlap the other?

Is there a way to showcase both conditions side by side on the same line? If Condition 1 is met, display the images; if Condition 2 is met, show the video. import React, { useState } from "react"; import { Card } from "react-bootstrap" ...

Switching an array with a single string in MongoDB

I'm facing an issue with grouping data in mongoDB and wondering if there's a way to transform an array into a string. The objects included in $lookup are currently displayed in an array format, but I need them in a single string. Here is my code ...

Utilizing Locale to Rewrite URLs in Next.js Version 13

I've been attempting to rewrite the URL based on the locale extracted from my middleware.js, but for some reason, the URL isn't being rewritten and leads to a page-not-found error 404. Strangely though, if I manually navigate to "localhost:3000/e ...

Angular 2 TypeScript: The concat() function operates as mutable within the framework

When I declare an array on an @Injectable provider, my intention is to use it across different components. normeList: any[] = [ { name: 'choice 1', type:'false' }, { name: 'choice 2', typ ...

What is the best method for adding files to JSZip from a remote URL?

Is it possible to load files into a Zip folder from a specified URL? For example: var zip = new JSZip(); zip.file("file.txt", "/site.net/files/file.txt"); Update I am following this example: I attempted the code provided but it was unsuccessful. I do ...

Dealing with the hAxis number/string dilemma in Google Charts (Working with Jquery ajax JSON data)

My Objective I am attempting to display data from a MySQL database in a "ComboChart" using Google Charts. To achieve this, I followed a tutorial, made some modifications, and ended up with the code provided at the bottom of this post. Current Behavior T ...

Automated tool for generating random JSON objects

Looking for a tool that can generate random JSON objects? I'm in need of one to test my HTTP POST requests and incorporate the random JSON object into them. Any recommendations? ...

Adjust image size as the page is resized

My challenge is to resize images that are typically too large for the current window size, ensuring they fit within 85% of the client window. I tried creating a function utilizing onload and onresize events but encountered issues. function adjustImages(){ ...

Looking for a way to include the current page's HTML code when exporting a CSV file

Here is the HTML code showcasing the Export button: <form method="post" name="download_csv" class="pull-right" enctype="multipart/form-data"> <button type="submit" name="Export" class="btn btn-success" >Export to excel</button> < ...

What is the best way to display content at a specific time using Angular?

Whenever a user searches something and no results are found, I display a message. However, when there is content to show, an error message pops up for about 3-4 seconds before disappearing and showing the search results. This is my HTML: <div> &l ...

The usage of Angular Tap is no longer recommended or supported

My Angular application contains the following HTTP interceptor: import { Observable } from 'rxjs'; import { Injectable } from '@angular/core'; import { HttpInterceptor, HttpResponse } from '@angular/common/http'; ...