The issue with the first slide count not functioning when clicking next on the Bootstrap 4 Carousel has not been resolved

After some trial and error, I managed to figure out how to display the slide/total count. However, there is a problem when clicking on next as it returns to the first slide but still shows 4/4 instead of 1/4.

JSFiddle: https://jsfiddle.net/zorw7yLe/

HTML:

<div class="num"></div>
<a class="next" href="#carouselExampleIndicators" role="button" data-slide="next"> -->>></a>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item item active">
      <img class="d-block w-100" src="https://via.placeholder.com/500x300" alt="First slide">
      <div class="carousel-caption">
        <h5>Title of the first image</h5>
        <p>Some description of the first image</p>
      </div>
    </div>
    <div class="carousel-item item">
      <img class="d-block w-100" src="https://via.placeholder.com/500x300" alt="Second slide">
      <div class="carousel-caption">
        <h5>Title of the second image</h5>
        <p>Some description for the second image</p>
      </div>
    </div>
    <div class="carousel-item item">
      <img class="d-block w-100" src="https://via.placeholder.com/500x300" alt="Third slide">
      <div class="carousel-caption">
        <h5>Title of the third image</h5>
        <p>Some description for the third image</p>
      </div>
    </div>
    <div class="carousel-item item">
      <img class="d-block w-100" src="https://via.placeholder.com/500x300" alt="Fourth slide">
      <div class="carousel-caption">
        <h5>Title of the fourth image</h5>
        <p>Some description for the fourth image</p>
      </div>
    </div>
  </div>
</div>

JS:

var totalItems = $('.carousel-item').length;
var currentIndex = $('.carousel-item.active').index() + 1;
var down_index;
$('.num').html(''+currentIndex+'/'+totalItems+'');

    $(".next").click(function(){
    currentIndex_active = $('.carousel-item.active').index() + 2;
    if (totalItems >= currentIndex_active)
    {
        down_index= $('.carousel-item.active').index() + 2;
        $('.num').html(''+currentIndex_active+'/'+totalItems+'');
    }
});

Answer №1

My recommendation is to utilize the bootstrap carousel events instead of directly monitoring clicks on your link:

https://getbootstrap.com/docs/4.0/components/carousel/#events

var totalItems = $('.carousel-item').length; 

$('#carouselExampleIndicators').on('slide.bs.carousel', function (e) {
    $('.num').html((e.to+1)+'/'+totalItems);
});

This approach ensures that your counter accurately reflects the current slide.

Additionally, be cautious with your condition for resetting the counter back to 1/4 as it might not work correctly in all scenarios. Instead, rely on the bootstrap events for a smoother experience.

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

methods to add an object to formData in vuejs

I am having trouble appending an object to FormData using Axios. I do not want to send data by JSON.stringify() data () { return { product: { title: '', description: '', properties: { ...

Achieve a seamless transition for the AppBar to extend beyond the bounds of its container in Material

I am finalizing my app structure by enclosing it within an MUI Container with maxWidth set to false. The issue I'm facing is that the AppBar inside the container doesn't span the full width of the screen due to paddingX property enforced by the c ...

Utilizing CSS to Display a Variety of Colors within a Text String

Is it possible to style different words in a sentence with various colors using only CSS, without using the span element? For instance, how can I make the word "Red" appear in red, "Blue" in blue, and "Green" in green within an h1 tag? ...

Tips for transitioning from custom CSS to Material UI's CSS in JS

I came across a project where someone implemented components with custom CSS. One interesting thing I noticed was a wrapper component, similar to Material UI's Container or just a simple div with applied styles. export const Container = styled.div` ...

What is the best way to create a fresh revision with every build in Vue.js?

Currently, I am utilizing vue cli along with pwa (workbox). Upon building the project, Vue automatically generates a file called: precache-manifest.882c44d211b70f8989278935.js. Within this file, there are entries for revision and url: { "revision": ...

Animating or transitioning CSS keyframes to an inline value when the page is initially loaded

In my current project, I am working on creating HTML and CSS-based bar representations of percentage values. The structure of the code is as follows: Using HTML: <div class="chart"> <div class="bar" style="width:43%"></div> </div&g ...

Guide on retrieving the ID after a new entry is added using a trigger in Express.js and MySQL

Currently, I am utilizing express and workbench to set up a database for handling the creation, viewing, and updating of cars. When I make a POST request to add a new car, it generates a new entry with details such as manufacturer, model, and price. I hav ...

"Utilizing Express.js and PHP for Streamlined Functionality

I have been working on a project that involves using expressjs, Php, Html, and MongoDB. Here is the code snippet from my "Index.Html" File: <form action="/Login" method="POST"> <input type="text" placeholder="name" name="Username"> <in ...

Alert: Angular 5 detects an invalid selector '';'

I am encountering an issue with my Angular 5 application. After running ng b --prod, I am seeing the following warnings: Warning in Invalid selector '; .space1x at 6219:39. Ignoring. The CSS style for space1x in style.css is as follows: .space1 ...

JavaScript is unable to create an Object for the ActiveX control, resulting in a 429 error

Recently, I developed an ActiveX control named ZPLPrint.cs which I then compiled into a dll using the csc command in the .Net 4.xxx directory. Following that, I registered the dll with the command regasm ZPLPrint.dll /tlb /codebase. My setup includes utili ...

Arranging elements in HTML for Manipulating with JavaScript

I haven't started coding yet, but I'm contemplating the overall strategy. My front end is primarily composed of HTML tables, giving it an Excel-like appearance. I am considering generating default pages and then using JavaScript to dynamically m ...

The AJAX response indicates data.Success as false even though there is data present in the response

The issue I am facing involves the JQuery Form Plugin and IE 11. For some reason, "data.Success" is returning false even though there is valid JSON in the response, confirmed through network tracing with IE Developer Tools. Upon making an initial AJAX POS ...

Is there a way to create distance between these icons?

<div id="unique-icons"> <a href="#"> <img class="github-logo1" src="GitHub-Mark-64px.png" alt="first github logo"> </a> <a href="#"> <i ...

Tips for extracting information from a String Object using the characters ' ' and '\':

When attempting to read the JSON data from AWS SNS, I encountered an issue with parsing the string object in jsonlog, resulting in a SyntaxError:Unexpected token \ in JSON at position 1 My attempts to replace '\n' and '\&bs ...

What is the best way to move between websites or pages without having to reload the current page using a selector?

Have you ever wondered how to create a webpage where users can navigate to other websites or pages without seeing their address, simply by selecting from a drop-down menu? Take a look at this example. Another similar example can be found here. When visit ...

Ways to incorporate lighting into the instancing shader

Is there a way to incorporate ambient and directional lighting into the shader when using InstancedBufferGeometry? For example, I am looking to add lighting effects to a specific instance, like in this example: Below is the vertex shader code: precision ...

Interacting with Selenium API using a jQuery event handler

I am working on a Java Selenium application that needs to respond to specific events occurring in an open browser window. One such event is when a user interacts with elements on the page, and I need my Java application to be aware of it so that it can tak ...

Unable to group the array based on the key value using Jquery or Javascript

I am looking to group my array values based on specific key values using Jquery/Javascript, but I am facing issues with my current code. Let me explain the code below. var dataArr=[ { "login_id":"9937229853", "alloc ...

Step-by-step guide on creating a sequential glowing effect for list items with CSS and JQuery

I would like to create a glowing effect on each icon individually. The idea is for each icon to glow for a brief moment and then return to its normal state before moving on to the next icon in line. I am considering using text-shadow for the glow effect an ...

What are some of the methods that can be used with the Facebook API to interact with the Like button

I am working on developing a JavaScript script that will automatically click on all the LIKE buttons for posts loaded on Facebook in the Chrome browser. Input: "" Or any other FB page,Groups,Profiles Step1: I will scroll down to load all the posts S ...