Tom Perkins
Tom Perkins

Reputation: 499

How to add a class to a parent element based on the child's class using jQuery

I'm creating a standard jQuery image slideshow, but I need to add a certain class to a container div, based on which slide is currently showing.

I'm using Flexslider which helpfully gives the current slide a class of 'flex-active-slide'. What I'd like to do is to use that class combined with a unique class to select the active slide (i.e. '.heating-slide.flex-active-slide).

I'd then like to have jQuery apply an additional class to the containing div (.image-slider.full-width), based on which slide is currently showing.

For example:

Here's my code:

<div class="image-slider full-width">
<div class="image-slider-container">
    <div class="flexslider wide">
        <ul class="slides">
            <li class="cooling-slide">
                <img src="/assets/images/image-slider/slides/cooling-slide.jpg" />
                <div class="flex-caption">
                    <h2 class="">Cooling</h2>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam</p>
                    <a class="button" href="#">Find out more</a>
                </div>
            </li>
            <li class="heating-slide">
                <img src="/assets/images/image-slider/slides/heating-slide.jpg" />
                <div class="flex-caption">
                    <h2 class="">Heating</h2>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam</p>
                    <a class="button" href="#">Find out more</a>
                </div>
            </li>
            <li class="ventilation-slide">
                <img src="/assets/images/image-slider/slides/ventilation-slide.jpg" />
                <div class="flex-caption">
                    <h2 class="">Ventilation</h2>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam</p>
                    <a class="button" href="#">Find out more</a>
                </div>
            </li>
        </ul>
    </div>
</div>
</div>

And here's my poor attempt at writing the required code:

<script type="text/javascript" >
$(document).ready(function () {
    $(".heating-slide.flex-active-slide") SOMETHING HERE (".image-slider.full-width").addClass("heating-container");
});
</script>

Any help you can offer will be appreciated!

Upvotes: 0

Views: 636

Answers (1)

Richard Dalton
Richard Dalton

Reputation: 35793

Update

I wasn't taking into account the other class that was on the element, also made it remove the previously added class so you don't end up with all of them on there:

$(function() {    
    function setContainer() {
        var activeSlide = $(".flex-active-slide");
        var activeSlideClass = activeSlide.attr('class')
                                            .replace('flex-active-slide', '')
                                            .replace("-slide", "-container")
        var slider = activeSlide.closest(".image-slider.full-width");
        var latestClass = slider.data("latest-class");

        if (latestClass) {
            slider.removeClass(latestClass);
        }
        slider.addClass(activeSlideClass)
              .data("latest-class", activeSlideClass);
    }

    $('.flexslider').flexslider({
        animation: "fade",
        controlsContainer: ".flex-container",
        animationSpeed: 800,            //Integer: Set the speed of animations, in milliseconds
        slideshowSpeed: 10000,           //Integer: Set the speed of the slideshow cycling, in milliseconds
        after: function() { setContainer(); } 
    });

    setContainer();
});

You should be able to paste that into your code as is instead of the existing flexslider binding.

Working Fiddle - http://jsfiddle.net/xt4Ym/3/

Upvotes: 1

Related Questions