diff --git a/js/src/carousel.js b/js/src/carousel.js index 68d11a32f2ab..dd2894e64ec2 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -274,8 +274,10 @@ class Carousel extends BaseComponent { const activeIndicator = SelectorEngine.findOne(SELECTOR_ACTIVE, this._indicatorsElement) - activeIndicator.classList.remove(CLASS_NAME_ACTIVE) - activeIndicator.removeAttribute('aria-current') + if (activeIndicator) { + activeIndicator.classList.remove(CLASS_NAME_ACTIVE) + activeIndicator.removeAttribute('aria-current') + } const newActiveIndicator = SelectorEngine.findOne(`[data-bs-slide-to="${index}"]`, this._indicatorsElement) diff --git a/js/tests/unit/carousel.spec.js b/js/tests/unit/carousel.spec.js index 2960eb5ce59f..c40f242bc00b 100644 --- a/js/tests/unit/carousel.spec.js +++ b/js/tests/unit/carousel.spec.js @@ -877,6 +877,35 @@ describe('Carousel', () => { }) }) + it('should handle indicators without an active indicator on initialization', () => { + return new Promise(resolve => { + fixtureEl.innerHTML = [ + '
' + ].join('') + + const carouselEl = fixtureEl.querySelector('#myCarousel') + const secondIndicator = fixtureEl.querySelector('#secondIndicator') + const carousel = new Carousel(carouselEl) + + carouselEl.addEventListener('slid.bs.carousel', () => { + expect(secondIndicator).toHaveClass('active') + expect(secondIndicator.getAttribute('aria-current')).toEqual('true') + resolve() + }) + + carousel.next() + }) + }) + it('should call next()/prev() instance methods when clicking the respective direction buttons', () => { fixtureEl.innerHTML = [ '