What steps are needed to activate the next and previous buttons in the bootstrap carousel?

Recently, I've taken an interest in using Bootstrap 5 for the design of my website. I wanted to incorporate a carousel for showcasing my coffee products, with only 3 items visible at a time when the navigation buttons are clicked.

Currently, the carousel container displays 3 items, but when I navigate using the buttons, the 4th item is not shown as intended. I'd like the carousel to rotate and include the 4th item while still displaying only 3 items at a time.

Below is the HTML code for my carousel:

<div class="container mt-5">
      <div class="carousel-container">
          <div id="carouselExampleControls" class="carousel slide" data-bs-ride="carousel">
              ... (carousel items) ...
          </div>
      </div>

Here is the link to my Bootstrap stylesheet:

<link href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">

And here are the scripts used:

 <script src="js/script.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

I tried adding a JavaScript function to address the issue:

JavaScript File: script.js

// Initialize the Bootstrap Carousel component without waiting for the entire document to load
var myCarousel = document.querySelector('#carouselExampleControls');
var carousel = new bootstrap.Carousel(myCarousel, {
  interval: false, // Disable automatic sliding
  wrap: false // Disable looping of carousel items
});

// Show/hide the fourth carousel item based on slide events
myCarousel.addEventListener('slide.bs.carousel', function (event) {
  ... (JavaScript logic) ...
});

Script in HTML:

<script src="js/script.js"></script>

This is the 4th item that I want to display when navigating through the carousel:

<!-- Fourth carousel item (hidden initially) -->
                          <div class="product-item d-none">
                              ... (fourth item details) ...
                          </div>

Answer №1

You may have initialized your script before the bootstrap framework is loaded.

Consider this alternative approach:

  <script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="33505c415673011d0a1d01">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1d3dedec5c2c5c3d0c1f1849f819f83">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"gt;
</script>
 <script src="js/script.js"></script>

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 impact does the spread operator have on the efficiency of code execution

Exploring two techniques for constructing an array of objects: Technique 1 (including all properties, even if duplicates exist): const employees = [ { company: 'ABC', country: 'IN', zip: 123, employeeId: 123, emp ...

Issue encountered with Ionic and ssh2: process.binding is not supported

I am currently delving into the world of Ionic and experimenting with creating a basic application that utilizes SSH2 to establish an ssh connection between the app and a server. Here is a breakdown of the steps I took to encounter the issue: Steps to Rep ...

Skipping MongoDB in loop operations in a Node.js environment.The original text was modified to

Apologies for the beginner question (The following code is related to express framework and mongoose DB) I am attempting to iterate through the array 'Users' which contains usernames. Then, I am trying to match them in the mongoose database to r ...

What is the process for loading a PHP page dynamically within the <DIV> of another PHP page?

At this moment, I have two PHP pages named test1.php and test2.php. In the test1.php page, there are 2 DIVs: one labeled "SubmitDiv" and the other "DisplayDiv". Inside the SubmitDiv resides a Submit button. The desired functionality is that when a user cli ...

The external hyperlink is not functional in Firefox and Chrome browsers, but it operates properly in Internet Explorer 9

For security reasons, it seems that embedding an external link for live scoring from itftennis.com on our website is blocked. If you try to click on the jsfiddle LIVE SCORING link, it will open a page with an error. However, if you click on the address ba ...

Installing and loading Node.js modules on the fly

I am currently developing a Node.js module called A that relies on another Node.js module, B, from NPM. With new versions of module B being released on NPM, I want my module A to automatically update to the latest version of module B (ensuring it always us ...

Need to double tap to remove item in redux-toolkit

Trying to delete an item in Redux Toolkit, but having trouble as the remove function only works on screen. I have to press twice to delete the previous one. Here is the reducer: const noteReducer = createSlice({ name: "note", initialState: N ...

Display a Bootstrap alert message in a Twig file upon submission

I may be new to this, but I'm looking for the easiest way to display a Bootstrap5 Alert after submitting a form without delving into JavaScript and all that complexity. Here's an example of what I currently have in the header of my HTML form in ...

Different ways to provide user feedback on a SPA website following AJAX requests

I have a single-page application website developed using React.js. What are some options for notifying the user of successful/failed/pending AJAX calls resulting from various user interactions? I am aware of Toastr-style messages that appear in the corner ...

What is the process of creating a new array by grouping data from an existing array based on their respective IDs?

Here is the initial array object that I have: const data = [ { "order_id":"ORDCUTHIUJ", "branch_code":"MVPA", "total_amt":199500, "product_details":[ { ...

Conceal the Material UI Grid element and allow the following Grid Item to smoothly transition into its place

I need to create a form where certain Textfields must be hidden. However, when I attempt to hide a Grid item within a Grid container using display: none, the next grid item does not move to take its place. Instead, it creates whitespace. Is there a soluti ...

The functionality of Javascript is being compromised when utilizing ng-repeat

Just recently diving into the world of AngularJs while developing a website. I've successfully retrieved data from Rest services on a page, and used ng-repeat to display it. The issue arises when I have a regular javascript element on the page that i ...

Navigating around potential type errors when passing data for chart.js can be challenging. Here are some strategies to

I'm currently working on an application that includes a chart, and I'm facing an issue while trying to populate the chart with data from my store. The error occurs when I attempt to pass the chartData object through props to the data property of ...

Extracting a text value from a div using jQuery instead of displaying it in an alert modal

Can someone help me figure out a simple way to display text in a div that indicates what has changed compared to an alert modal with the following code? I'm struggling to understand it. Thanks... $(function(){ var abc = $('selec ...

Adjust the sizes of the points according to the level of zoom

I've been working on creating a dynamic map in d3.js that displays US Science funding agencies as points, with their sizes scaling based on zoom level. I referred to this starter kit for guidance. While there are other solutions out there, they often ...

CSS following a clicked event

http://codepen.io/anon/pen/GqmEAq My goal is to make the 'x' button clicked (the 'x' is created after clicking the plus sign on the menu) and then bring the dropdown back up. I have attempted: a:after:checked ~ .submenu{ max-height ...

Unable to retrieve options from a particular select box

Utilizing cheerio and nodejs to scrape all the countries listed on a website, I have implemented the following code: const rp = require('request-promise'); const cheerio = require('cheerio'); const options = { uri: 'https://u ...

Most effective method for incorporating external content onto website without using iframes (all websites are owned by me)

Managing a collection of websites is no easy task, especially when they are all powered by WordPress. One common feature among these sites is a simple "name, graphic, and social media links" page located at foo.domain.com/partner. Now, I'm looking to ...

Adding a picture to the webpage and saving it temporarily on the site

After exploring all options on the site, testing it rigorously and closely following the instructions provided, I am still unable to determine where exactly I went wrong. Website Link: The main goal is to upload an image and temporarily store it within t ...

Angular's Enhanced Feature: Selecting Multiple Columns

Looking for an open-source Angular library that can display items in multiple columns rather than each item spanning across multiple columns. Many libraries support tabular display, but the challenge is to find one that arranges items in multiple columns. ...