Bird
Raised Fist0
Bootsrapmarkup~7 mins

Carousel programmatic control in Bootsrap

Choose your learning style10 modes available
Introduction

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.

You want to start or stop the carousel automatically with code.
You want to move to a specific slide when a user clicks a custom button.
You want to pause the carousel when the user hovers over it.
You want to control the carousel from outside the carousel area.
You want to sync the carousel with other page actions or animations.
Syntax
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.

Examples
This moves the carousel to the next slide.
Bootsrap
var myCarousel = document.querySelector('#myCarousel');
var carousel = new bootstrap.Carousel(myCarousel);
carousel.next();
This jumps the carousel to the first slide (index 0).
Bootsrap
carousel.to(0);
This stops the carousel from sliding automatically.
Bootsrap
carousel.pause();
This starts the carousel sliding automatically again.
Bootsrap
carousel.cycle();
Sample Program

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.

Bootsrap
<!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>
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. What does programmatic control of a Bootstrap carousel allow you to do?
easy
A. Move slides using JavaScript commands
B. Change the carousel's color scheme
C. Add new images to the carousel dynamically
D. Automatically resize the carousel on window resize

Solution

  1. Step 1: Understand programmatic control meaning

    Programmatic control means controlling the carousel with code, not just user clicks.
  2. Step 2: Identify what JavaScript commands do

    JavaScript commands let you move slides forward, backward, or jump to a specific slide.
  3. Final Answer:

    Move slides using JavaScript commands -> Option A
  4. Quick Check:

    Programmatic control = move slides with code [OK]
Hint: Programmatic means controlling with code, not style [OK]
Common Mistakes:
  • Confusing programmatic control with styling changes
  • Thinking it adds images automatically
  • Assuming it resizes carousel automatically
2. Which JavaScript method correctly moves a Bootstrap carousel to the next slide?
easy
A. carousel.next()
B. document.carousel.nextSlide()
C. carousel.slideNext()
D. $('#carouselExample').carousel('next')

Solution

  1. Step 1: Recall Bootstrap carousel JavaScript syntax

    Bootstrap uses jQuery style calls like $('#id').carousel('method').
  2. Step 2: Identify correct method for next slide

    The method to go to next slide is 'next', so $('#carouselExample').carousel('next') is correct.
  3. Final Answer:

    $('#carouselExample').carousel('next') -> Option D
  4. Quick Check:

    Use $('#id').carousel('next') to move next [OK]
Hint: Bootstrap carousel uses $('#id').carousel('method') syntax [OK]
Common Mistakes:
  • Using non-existent methods like carousel.next()
  • Trying to call methods on document.carousel
  • Using camelCase method names not in Bootstrap API
3. Given this code snippet, what slide will be shown after execution?
$('#myCarousel').carousel(2);

Assuming the carousel has 4 slides indexed 0 to 3.
medium
A. Slide 0
B. Slide 1
C. Slide 2
D. Slide 3

Solution

  1. Step 1: Understand carousel method with number argument

    Calling carousel(n) moves the carousel to slide number n (zero-based index).
  2. Step 2: Identify which slide index 2 refers to

    Index 2 means the third slide (0,1,2), so slide 2 will be shown.
  3. Final Answer:

    Slide 2 -> Option C
  4. Quick Check:

    carousel(2) shows slide index 2 [OK]
Hint: carousel(n) jumps to slide n (zero-based) [OK]
Common Mistakes:
  • Thinking carousel(2) moves 2 slides forward
  • Confusing slide numbers starting at 1
  • Assuming carousel(2) triggers next slide
4. What is wrong with this code to pause a Bootstrap carousel?
document.getElementById('carouselExample').carousel('pause');
medium
A. The method name should be 'stop' instead of 'pause'
B. carousel method is not available on DOM element directly
C. Missing parentheses after 'carousel'
D. The ID selector should use '#' in getElementById

Solution

  1. Step 1: Understand how Bootstrap carousel methods are called

    Bootstrap carousel methods are called on jQuery objects, not plain DOM elements.
  2. Step 2: Identify why getElementById fails

    getElementById returns a DOM element without jQuery methods, so .carousel('pause') is undefined.
  3. Final Answer:

    carousel method is not available on DOM element directly -> Option B
  4. Quick Check:

    Use jQuery selector, not getElementById, for carousel methods [OK]
Hint: Use jQuery selector $('#id'), not getElementById, for carousel [OK]
Common Mistakes:
  • Trying to call carousel on plain DOM element
  • Using wrong method name 'stop' instead of 'pause'
  • Adding '#' inside getElementById argument
5. You want to programmatically jump to the last slide of a Bootstrap carousel with 5 slides. Which code snippet correctly does this?
hard
A. $('#carouselExample').carousel(4);
B. $('#carouselExample').carousel('last');
C. $('#carouselExample').carousel('to', 5);
D. $('#carouselExample').carousel.slide(5);

Solution

  1. Step 1: Determine last slide index for 5 slides

    Slides are zero-indexed, so last slide index is 4 (0 to 4).
  2. 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.
  3. Final Answer:

    $('#carouselExample').carousel(4); -> Option A
  4. Quick Check:

    Use carousel(lastIndex) to jump to last slide [OK]
Hint: Last slide index = total slides - 1; use carousel(index) [OK]
Common Mistakes:
  • Using 'last' as method which doesn't exist
  • Passing slide number out of range (5 instead of 4)
  • Using non-existent carousel.slide() method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard