"Creating Interactive Slider Bullets: A Guide to Linking them to Their Respective

I can't seem to figure out how to connect my slider bullets to my slider slides. I want bullet 1 to correspond to slide 1, bullet 2 to slide 2, and so on. It should be a simple task, but I'm struggling to make it work. I have set up a basic reset function that removes the active class from both the bullet and the slide when clicked. Now, I need to add the active class to the clicked bullet and its matching slide.

Any help with this would be greatly appreciated.

var slideBullet = document.querySelectorAll('.slider__bullet');
for (var i = 0; i < slideBullet.length; i++) {
  slideBullet[i].addEventListener('click', function() {
    var sliderSlide = document.querySelectorAll('.slider__slide');
    function resetSlides() {
      for (var i = 0; i < sliderSlide.length; i++) {
        sliderSlide[i].classList.remove('active');
      }
      for (var i = 0; i < slideBullet.length; i++) {
        slideBullet[i].classList.remove('active');
      }
    }
  });
}
  .slider {
  width: 90%;
  height: 100vh;
  position: relative;
  border: 1px solid black;
  margin: 0 auto;
}

.slider__slides {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: none;
  text-align: center;
  margin: 0 auto;
}

.slider__slides.active {
  display: block;
}

.slider__bullets {
  position: absolute;
  bottom: 5%;
  left: 0;
  right: 0;
  margin-left: auto;
  margin-right: auto;
  display: flex;
  justify-content: center;
}

.slider__bullet {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background-color: #a3a3a3;
  cursor: pointer;
  margin-left: 10px;
}

.slider__bullet.active {
  background-color: grey;
  transform: scale(1.8);
<div class="slider">
  <div class="slider__slides active">1</div>
  <div class="slider__slides">2</div>
  <div class="slider__slides">3</div>
  <div class="slider__slides">4</div>
  <div class="slider__slides">5</div>
  <div class="slider__bullets">
    <div class="slider__bullet active"></div>
    <div class="slider__bullet"></div>
    <div class="slider__bullet"></div>
    <div class="slider__bullet"></div>
    <div class="slider__bullet"></div>
  </div>
</div>

Answer №1

I've made some updates to your jquery code, Please try the code below to see if it solves your issue

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("slider__slides");
  var dots = document.getElementsByClassName("slider__bullet");
  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";
}
 .slider {
  width: 90%;
  height: 100vh;
  position: relative;
  border: 1px solid black;
  margin: 0 auto;
}

.slider__slides {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: none;
  text-align: center;
  margin: 0 auto;
}

.slider__slides.active {
  display: block;
}

.slider__bullets {
  position: absolute;
  bottom: 5%;
  left: 0;
  right: 0;
  margin-left: auto;
  margin-right: auto;
  display: flex;
  justify-content: center;
}

.slider__bullet {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background-color: #a3a3a3;
  cursor: pointer;
  margin-left: 10px;
}

.slider__bullet.active {
  background-color: grey;
  transform: scale(1.8);
  }
<div class="slider">
  <div class="slider__slides active">1</div>
  <div class="slider__slides">2</div>
  <div class="slider__slides">3</div>
  <div class="slider__slides">4</div>
  <div class="slider__slides">5</div>


  <div class="slider__bullets">
    <div class="slider__bullet active" onclick="currentSlide(1)"></div>
    <div class="slider__bullet" onclick="currentSlide(2)"></div>
    <div class="slider__bullet" onclick="currentSlide(3)"></div>
    <div class="slider__bullet" onclick="currentSlide(4)"></div>
    <div class="slider__bullet" onclick="currentSlide(5)"></div>
  </div>
</div>

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

Implementing React.JS to dynamically update a value from a websocket and store it within

I want to create a dynamic stock price table that updates with live stock prices using websockets. I have all the necessary data and need to update my stock price object with the live price. How can I access the variable and insert the value into the obj ...

Navigate through content easily using the arrow keys on your keyboard with the help of Easy Slider

I'm attempting to modify the Easy Slider in order to enable navigation of the slideshow with the arrow keys on the keyboard. I made adjustments to the javascript's animate function, changing it from: default: t = dir; break; ...to: default: t ...

Show a loading progress image during the page loading process (not during form submission)

Is there a way to show a loading GIF image while a page is loading or during postbacks using jQuery and JavaScript for long running tasks or processes that take a significant amount of time to execute? I attempted a solution but the loading image is not d ...

Ways to access elements and their associated values in a JavaScript Array

I'm facing a challenge with a JavaScript array where I need to extract teams (Team A, Team B) and organize them into one array while preserving their order. See below for the current output of the array, as well as the JS code provided and the expecte ...

Bower Error: No tag found for ENORESTARGET

I'm encountering an issue with installing dependencies for bower bootstrap material design. I am facing a problem specifically with version 0.5.10, as it displays an error message ENORESTARGET No tag found that was able to satisfy ^0.5.10. When attemp ...

Is there a way to stop text from wrapping between words and punctuation marks using CSS or jQuery?

There is a paragraph containing some text. One issue I am facing is that punctuation at the end of a word may get wrapped to the next line, as shown here: This is the sentence, This is a new line Is there a way to fix this using CSS or jQuery? ...

Capturing Key Events on Canvas in Vue.js

When using Vue 2.5, I encountered an issue with a canvas element that has a key event listener: <template> <canvas v-on:keyup.esc="abortThing"></canvas> </template> <script> export default { methods: { abortTh ...

Angular4: The Process of Iterating Through an Array of Objects within an Object

I'm struggling to iterate through an Object that contains an array of objects within an anchor tag. Here's how my code is structured: {"1":{"uri":"test1","name":"test1","icon":"http:\/\/dummyurl\/images\/icons\/test1-ico ...

Is there a way to verify if an email is already registered within a MERN stack application

I am in the process of creating a registration form and need to verify if an email already exists within the system. Below is the React code snippet showcasing the structure for better understanding. In the schema, emails are defined as unique. AuthContr ...

Transform a Static Page into an Interactive Design

As a novice in web design, I have created a page using HTML and CSS. Now, I am looking to make my static page responsive, so that the styling remains consistent when the browser window is resized. Below are the dimensions of my body: body{ width:100%; ...

Press the Enter key to generate a new table row

I have been dynamically creating an HTML table. Each row contains two columns created through a recursive call. Currently, a new row is generated by clicking on the second cell, but I want to switch this action to the "Enter" key press. Although my code su ...

Issues have been encountered with the Angular Bootstrap accordion animation not functioning properly when closing in a production

After setting up Bootstrap in Angular app, I encountered an issue in production. When using the accordion feature with animation, the closing animation does not work when I run ng build --configuration production and view it on the local server. The openin ...

Guide on plotting latitude and longitude coordinates on Google Maps with Vue.js through JSON data fetched via AJAX

I have implemented a Vue.js script to fetch JSON data through an AJAX request. However, I am encountering issues with the code. <script> new Vue({ el: '#feed' , data: { details: [], }, mounted() { this.$nextTick(fu ...

Utilizing AJAX in Cordova to Generate Dynamic JavaScript Charts from JSON Data

Exploring the world of Cordova, I have delved into utilizing canvas Js for visualizing data in chart format. Following a helpful tutorial, I created a Json file and inserted the specified data as instructed, following each step diligently. The outcome matc ...

Trouble displaying events from resource in Angular UI calendar integration

I have encountered a perplexing issue with Angular UI Calendar. My goal is to fetch events from an API and display them on the calendar. Initially, the events load correctly when the page is first loaded. However, if I navigate to the next month and then r ...

What is the best way to extract specific values from one JSON object and transfer them to another using lodash?

//I have a pair of objects var obj1={ "name":"mayur", "age":23 } var obj2={ "name":"keyur", "age":29, "limit":54, "surname":"godhani" } //I am familiar with one approach var j1 = {name: 'Varun', age: 24}; var j2 = {code: &ap ...

What is the best way to interpret HTML special characters (such as those with accents) in a Facebook share post?

<meta property="og:title" content="<?php echo $title; /> where $title is retrieved from a database. The title should display as blácv with the character a accented, but when sharing the post on Facebook, it appears as bl&aacutecv. T ...

When an if statement is triggered by an overload of information, it initiates the start of a fresh

I am in need of creating an if statement that will trigger whenever images with IDs 0 to 3 (a total of 4 images) are loaded. This if statement should then generate a new row containing 0 to 3 images, and the same logic needs to be applied for words as well ...

Is the three.js.master folder necessary for utilizing OBJLoader2.js? I keep getting a 404 error when trying to access it

As I venture into using three.js, I am attempting to import an OBJ file using OBJLoader2.js locally (without npm). However, I am encountering a 404 error for three.module.js, MeshReceiver.js, and OBJLoaderParser when trying to add import {OBJLoader2} from ...

Unable to append a property to each object within an array during a loop

Hey everyone, I could really use some help with this problem I'm facing. Let me explain what's going on - I'm working on pulling documents from MongoDB using Mongoose. Take a look at the array of objects that is returned from the mongoose qu ...