📖 Scenario: You are building a simple image carousel for a travel website homepage. The carousel will show three beautiful travel photos. Instead of relying on the default controls, you want to add your own buttons to move the carousel forward and backward programmatically.
🎯 Goal: Create a Bootstrap carousel with three images and add two custom buttons below it. These buttons will control the carousel to move to the previous or next slide when clicked.
📋 What You'll Learn
Use Bootstrap 5 carousel component with exactly three slides
Add two buttons labeled 'Previous Slide' and 'Next Slide' below the carousel
Use JavaScript to programmatically control the carousel when these buttons are clicked
Ensure the carousel cycles through slides correctly when buttons are used
Use semantic HTML and accessible button labels
💡 Why This Matters
🌍 Real World
Custom carousel controls are common on websites to match branding or improve user experience beyond default controls.
💼 Career
Knowing how to programmatically control UI components like carousels is important for front-end developers building interactive web pages.
Progress0 / 4 steps
1
Create the Bootstrap carousel with three slides
Write the HTML code to create a Bootstrap carousel with the id travelCarousel. Add exactly three carousel-item elements inside the carousel, each containing an <img> tag with src attributes set to "img1.jpg", "img2.jpg", and "img3.jpg" respectively. The first carousel-item must have the class active. Include the necessary Bootstrap classes for the carousel to function.
Bootsrap
Hint
Remember to add the active class only to the first carousel-item. Use Bootstrap classes carousel, slide, and carousel-inner as shown in Bootstrap documentation.
2
Add two buttons to control the carousel
Below the carousel div, add two <button> elements with the classes btn btn-primary. The first button must have the id prevBtn and the text Previous Slide. The second button must have the id nextBtn and the text Next Slide. These buttons will be used to control the carousel programmatically.
Bootsrap
Hint
Use <button> tags with the Bootstrap classes btn btn-primary. Assign the exact ids prevBtn and nextBtn to the buttons.
3
Create JavaScript variables to select carousel and buttons
Write JavaScript code to select the carousel element with id travelCarousel and store it in a constant called carouselElement. Then create a Bootstrap Carousel instance called carousel using new bootstrap.Carousel(carouselElement). Also select the buttons with ids prevBtn and nextBtn and store them in constants prevButton and nextButton respectively.
Bootsrap
Hint
Use document.getElementById to select elements by their ids. Create the Bootstrap carousel instance with new bootstrap.Carousel().
4
Add event listeners to buttons to control carousel slides
Add click event listeners to prevButton and nextButton. When prevButton is clicked, call carousel.prev() to move to the previous slide. When nextButton is clicked, call carousel.next() to move to the next slide.
Bootsrap
Hint
Use addEventListener on each button to listen for click events. Inside the event handler, call carousel.prev() or carousel.next() accordingly.
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
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 A
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?