A carousel lets you show multiple images or content slides in one place. Programmatic control means you can change slides using code, not just by clicking buttons.
Carousel programmatic control in Bootsrap
var myCarousel = document.querySelector('#myCarousel'); var carousel = new bootstrap.Carousel(myCarousel, { interval: 2000, wrap: true }); // To go to next slide carousel.next(); // To go to previous slide carousel.prev(); // To go to a specific slide (zero-based index) carousel.to(2); // To pause the carousel carousel.pause(); // To cycle (start) the carousel carousel.cycle();
You first select the carousel element by its ID or class.
Use the bootstrap.Carousel constructor to create a control object.
var myCarousel = document.querySelector('#myCarousel');
var carousel = new bootstrap.Carousel(myCarousel);
carousel.next();carousel.to(0);carousel.pause();
carousel.cycle();
This example shows a Bootstrap carousel with three slides. Below it are four buttons to control the carousel using JavaScript: Previous, Next, Pause, and Play. Clicking these buttons moves the slides or controls the automatic sliding.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Carousel Programmatic Control</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <main class="container py-4"> <h1>Carousel Programmatic Control</h1> <div id="myCarousel" class="carousel slide" data-bs-ride="carousel"> <div class="carousel-inner"> <div class="carousel-item active"> <img src="https://via.placeholder.com/600x300?text=Slide+1" class="d-block w-100" alt="Slide 1" /> </div> <div class="carousel-item"> <img src="https://via.placeholder.com/600x300?text=Slide+2" class="d-block w-100" alt="Slide 2" /> </div> <div class="carousel-item"> <img src="https://via.placeholder.com/600x300?text=Slide+3" class="d-block w-100" alt="Slide 3" /> </div> </div> </div> <div class="mt-3"> <button id="prevBtn" class="btn btn-primary me-2">Previous</button> <button id="nextBtn" class="btn btn-primary me-2">Next</button> <button id="pauseBtn" class="btn btn-warning me-2">Pause</button> <button id="playBtn" class="btn btn-success">Play</button> </div> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <script> const myCarousel = document.querySelector('#myCarousel'); const carousel = new bootstrap.Carousel(myCarousel, { interval: 3000, wrap: true }); document.getElementById('prevBtn').addEventListener('click', () => { carousel.prev(); }); document.getElementById('nextBtn').addEventListener('click', () => { carousel.next(); }); document.getElementById('pauseBtn').addEventListener('click', () => { carousel.pause(); }); document.getElementById('playBtn').addEventListener('click', () => { carousel.cycle(); }); </script> </body> </html>
Make sure to include Bootstrap's CSS and JS files for the carousel to work.
Programmatic control lets you add custom buttons or actions outside the carousel.
Use descriptive alt text on images for accessibility.
Programmatic control lets you move carousel slides using JavaScript.
You can start, stop, go next, previous, or jump to a slide with code.
This helps create interactive and custom carousel experiences.
Practice
Solution
Step 1: Understand programmatic control meaning
Programmatic control means controlling the carousel with code, not just user clicks.Step 2: Identify what JavaScript commands do
JavaScript commands let you move slides forward, backward, or jump to a specific slide.Final Answer:
Move slides using JavaScript commands -> Option AQuick Check:
Programmatic control = move slides with code [OK]
- Confusing programmatic control with styling changes
- Thinking it adds images automatically
- Assuming it resizes carousel automatically
Solution
Step 1: Recall Bootstrap carousel JavaScript syntax
Bootstrap uses jQuery style calls like $('#id').carousel('method').Step 2: Identify correct method for next slide
The method to go to next slide is 'next', so $('#carouselExample').carousel('next') is correct.Final Answer:
$('#carouselExample').carousel('next') -> Option DQuick Check:
Use $('#id').carousel('next') to move next [OK]
- Using non-existent methods like carousel.next()
- Trying to call methods on document.carousel
- Using camelCase method names not in Bootstrap API
$('#myCarousel').carousel(2);Assuming the carousel has 4 slides indexed 0 to 3.
Solution
Step 1: Understand carousel method with number argument
Calling carousel(n) moves the carousel to slide number n (zero-based index).Step 2: Identify which slide index 2 refers to
Index 2 means the third slide (0,1,2), so slide 2 will be shown.Final Answer:
Slide 2 -> Option CQuick Check:
carousel(2) shows slide index 2 [OK]
- Thinking carousel(2) moves 2 slides forward
- Confusing slide numbers starting at 1
- Assuming carousel(2) triggers next slide
document.getElementById('carouselExample').carousel('pause');Solution
Step 1: Understand how Bootstrap carousel methods are called
Bootstrap carousel methods are called on jQuery objects, not plain DOM elements.Step 2: Identify why getElementById fails
getElementById returns a DOM element without jQuery methods, so .carousel('pause') is undefined.Final Answer:
carousel method is not available on DOM element directly -> Option BQuick Check:
Use jQuery selector, not getElementById, for carousel methods [OK]
- Trying to call carousel on plain DOM element
- Using wrong method name 'stop' instead of 'pause'
- Adding '#' inside getElementById argument
Solution
Step 1: Determine last slide index for 5 slides
Slides are zero-indexed, so last slide index is 4 (0 to 4).Step 2: Use correct method to jump to slide by index
Calling carousel(4) jumps to slide index 4. Other options use invalid methods or indexes.Final Answer:
$('#carouselExample').carousel(4); -> Option AQuick Check:
Use carousel(lastIndex) to jump to last slide [OK]
- Using 'last' as method which doesn't exist
- Passing slide number out of range (5 instead of 4)
- Using non-existent carousel.slide() method
Ready to prove it?
Pass the test and this pattern moves unknown
