Determine the cumulative and present slide count on the Bootstrap slider

Using JavaScript and Bootstrap controls, you can easily track the number of slides in a standard Bootstrap carousel. By implementing a simple button press counter that increments or decrements, you can keep track of the total and current slide numbers using the .num class. However, the code below did not produce the desired outcome:

 var totalItems = $('.item').length;
            var currentIndex = $('div.item.active').index() + 1;

            var dynamicIndex;
            $('.num').html(''+currentIndex+'/'+totalItems+'');

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

                $(".prev").click(function(){
                    dynamicIndex = dynamicIndex - 1;
                if (dynamicIndex >= 1 )
                {
                    $('.num').html(''+dynamicIndex+'/'+totalItems+'');
                }
            });

Answer №1

It is important to use the correct jQuery selector, which in this case should be .carousel-item instead of .item...

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+'');
    }
});

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 withCredentials property to utilize the latest ES6 integrated HTTP request tool known as Fetch

What is the correct way to set withCredentials=true for a fetch promise return? fetch(url,{ method:'post', headers, withCredentials: true }); It seems like the MDN documentation covers everything about http-requesting, except for the u ...

Issue with off-canvas menu functionality on iPhone 5

My client believes that the off canvas menu on my website is not functioning properly. Despite no errors appearing in the console, I am unable to test it myself as I do not own an iPhone or iPad. Would anyone be able to help troubleshoot this? View demo he ...

unable to insert logo into HTML

I am customizing an HTML template, but the dimensions of the logo in the template are smaller than my logo. Despite adding my logo to the template, it is not showing up. @media (max-width: 673px) { #logo { margin-left: 20px; } } #header #logo ...

Issue with implementing Bootstrap 4 Scrollspy feature in MVC Core frameworks

As I delve into learning MVC Core, I've been attempting to incorporate Scrollspy from Bootstrap 4. However, it appears that the functionality is not working as expected. Despite scouring through various questions on this platform, none of the solution ...

Utilizing conditional statements and database inquiries

My goal is to simplify this process as much as possible. When a user clicks the submit button, the following steps take place: Validate if the credit card/expiry date/CVC value are valid Generate a token as an input Post the value of the token in the PHP ...

Using both PHP and HTML to create a select tag

I'm new to PHP and HTML and I'm trying to create a form with three drop-down lists (select tags). However, when I select an option from one list, the values in the other two lists disappear. Should I save the selected values when submitting the f ...

CSS problem with padding and width

Currently, I'm working on a conditional rendering issue where I want to give a div full width but due to padding applied to its parent, it's not expanding to the container's full width. Here is the snippet of the code: import Image from &qu ...

Calculating the rotation angle of a spinning cylinder in Three.js animations

I'm struggling with this Math problem and my skills are failing me. To see my progress so far, you can view the working example here. After extracting the y and z positions from the rotating cylinder, I've managed to pause the animation when the ...

Ways to view the outcome of json_encode function?

In PHP, I have an array that looks like this: $user_data = Array ( [session_id] => 30a6cf574ebbdb11154ff134f6ccf4ea [ip_address] => 127.0.0.1 [user_agent] => Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1 [las ...

Achieving full-width container on mobile devices with Bootstrap

Despite my efforts in the CSS file, I am still struggling to get the container to take up the full width on mobile devices. Here is my CSS code: @media (max-device-width: 1024px) { .col-sm-2{ width: 100%; } .container { margin: 0 auto; width: ...

Show the content of a div inside a tooltip message box in an MVC application

I have a MVC application where I need to show tooltip messages when hovering over my HTML div. HTML: <li class="Limenu" data-placement="right"> <span class="LessMenuspan btn-primary" pid="@i.ConsumerNo_" onmouseover="FacebookprofileTip(1, 0,thi ...

Is it possible to run a web game on the same URL?

While working on quickly prototyping my ideas, I have been utilizing Express with Mongo and have successfully implemented a mongostore cookie storage system. Here's what I'm wondering: Can I keep everything happening on one page, specifically &a ...

What is the best way to save geolocation coordinates in a Javascript array?

I am attempting to utilize HTML5 geolocation to determine a user's location and then store the latitude and longitude coordinates in an array for future use with Google Maps and SQL statements. However, when I attempt to add these coordinates to the a ...

CORS policy is preventing preflight requests to Cloud functions using the OPTIONS method

Currently, I am working on a project to develop a basic app where users can upload a .pdf file on the front end. The server will receive the file and process it until it generates a Firebase storage link. The front end is hosted on Firebase Hosting, while ...

How can I ensure that a list of cards is 100% in height?

Can anyone assist me in setting the height of my grid cards? I am struggling to adjust the height of my grid card list, despite setting the parent element's height as well <div class="desc-grid__body cards-grid"> <ul clas ...

Clear all CSS styles applied to a targeted division

My website built on Wordpress links to several CSS files. One specific div has its own unique style that is separate from the main Wordpress styles. Unfortunately, the Wordpress CSS ends up interfering with my custom div's layout. Is there a way in HT ...

Creating interactive text using Three.js and dat.gui

Exploring the possibilities of dynamic rendering user inputted text with three.js and dat.gui, I have developed a code snippet to display the text: var displayGui = function(){ var gui = new dat.GUI(); var parameters = { message:"sample", spin ...

Align columns responsively with Bootstrap and center overlay image

Experiencing issues with aligning responsive divs using Bootstrap. Here is the code in question: <div class="container-fluid"> <div class="row"> <div class="col-xl-5"> <img src=&quo ...

Is it possible to utilize a hyphen in the link_to attribute?

I'm encountering a dilemma in my Rails app where I need to assign a value to a custom data-* attribute for an anchor tag. The issue is that hashes can't contain hyphens, leading to difficulties with this code: <%= link_to 'Example', ...

Using a Vue computed property updates the values within the data object

My data structure (this.terms) looks like this: { name: 'First Category', posts: [ { name: 'Jim James', tags: [ 'nice', 'friendly' ] }, ...