Meeps Meeps - 1 year ago 97
Javascript Question

How to target bootstrap carousel's active slide?


Apologies for this likely being a really stupid question, I'm still learning!

I'm trying to target the active slide in the bootstrap carousel to perform an action. I'd like it to trigger some style changes for each of the three slides i.e. changing button colours etc.

Earlier I tried just targeting the divs by their numbers(

#1, #2, #3
), but kept getting stuck there too, it looked something like this;

var shead = document.getElementById("sub-head");
if ($('#1').is(':visible') && $('#2').is(':hidden') && $('#3').is(':hidden')) { = "blue";

I repeated this for each of the slides using
for each of the divs accordingly, although I only ever seemed to get stuck with the last presentation of the style colour change.

I did a some searching on this and I've seen people using
, but I just seem to keep hitting dead ends, can anyone give me a hand with this, not sure why it's not catching, any guidance would be appreciated?


<header id="Carousel" class="carousel slide carousel-fade">

<ol class="carousel-indicators">
<li data-target="Carousel" data-slide-to="0" class="active"></li>
<li data-target="Carousel" data-slide-to="1"></li>
<li data-target="Carousel" data-slide-to="2"></li>

<div class="carousel-inner">
<div class="item active" id="1"></div>
<div class="item" id="2"></div>
<div class="item" id="3"></div>



if ($('#Carousel').carousel(1).hasClass('active')) {
alert("this is an alert");

Answer Source

Bootstrap has custom events you can hook to.

This event will fire when the method is going to slide.

$('#Carousel').on('', function (ev) {
  var id =;
  switch (id) {
    case "1":
      // do something the id is 1
    case "2":
      // do something the id is 2
    case "3":
      // do something the id is 3
      //the id is none of the above

There is also a event. This event is invoked when the slider is finished sliding.

You can read about the differences here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download