3

Does anyone know how to override the default bootstrap carousel functionality of keyboard left and right buttons working only after first clicking one of the arrows on the screen.

I have rows of cards which when clicked opens a modal and inside that modal I have my carousel. I'd like the user to immediately be able to use the keyboard arrows and not have to click the arrows on the screen to enable the keyboard arrows to work. This appears to be the default behavior.

I've tried using $(#exampleId).focus() on various elements in the modal and carousel but no luck getting it to work thus far.

The default behavior can be seen in the bootstrap carousel doc: https://getbootstrap.com/docs/5.0/components/carousel/#with-controls

Bootstrap-5 Carousel with controls code:

<div id="carouselExampleControls" class="carousel slide" data-bs-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
    </div>
  </div>
  <button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleControls" data-bs-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Previous</span>
  </button>
  <button class="carousel-control-next" type="button" data-bs-target="#carouselExampleControls" data-bs-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Next</span>
  </button>
</div>

Edit: After following bootstrap documentation link you'll see that you can only slide the carousel using the keyboard after clicking one of the controls on the carousel "<" or ">". If you just start by using your left or right arrow keyboard buttons the carousel doesn't respond.

BootDev
  • 146
  • 2
  • 9
  • Please post the code to repro the issue – Carol Skelly Jun 23 '21 at 14:50
  • Thank you for the reply, if you follow the bootstrap link you'll see what I mean. You can only slide the carousel using the keyboard _after_ clicking one of the controls on the carousel "<" or ">". If you just start by using your left or right arrow keyboard buttons the carousel doesn't respond. – BootDev Jun 23 '21 at 15:05
  • This is not an error nor is it an issue. If you expect to use the keyboard to control the carousel, you can expect users to navigate to the carousel by using the keyboard aswell. Focus the text in front of the carousel, press tab, now you can use the arrow keys. Simple misunderstanding and expectations. – Tim Vermaelen Jun 30 '21 at 19:12
  • Any solution found? I struggle with the same problem. – Michael Fehr Jun 08 '22 at 22:47

0 Answers0