Search code examples
jqueryclassonclickfooter

jQuery add class onclick


Hi am currently using the code below for an expandable footer on my site.

I would like to adapt it so that when the link class '.toggle' is clicked it will add a class called '.is-open' to the element so far the code does this but then additionally I would like the code to add the class '.is-open' to another class called '.footercontrol'

$(".footer, .toggle:not(:first-child)").hide();

$(".toggle").click(function(e) {

e.preventDefault();

if ( $(this).hasClass("is-open") ){
    $(this).removeClass("is-open").nextAll(".footer, .toggle:not(:first-         child)").slideUp(500).removeClass("is-open");
    return;
}


$(this).toggleClass("is-open");
$(this).next(".footer").slideToggle(500).next(".toggle").slideToggle(500);

$("html, body").animate({
    scrollTop: $(document).height()
}, 500);

});

Thanks in advance :)


Solution

  • managed to sort it myself in the end thanks to both your help, I simply amended the existing code and added another class to the existing events like so:

    $(".footer, .toggle:not(:first-child)").hide();
    
    $(".toggle, .footercontrol").click(function(e) {
    
    e.preventDefault();
    
    if ( $(this).hasClass("is-open") ){
        $(this).removeClass("is-open").nextAll(".footer, .toggle:not(:first-child),   .footercontrol").slideUp(500).removeClass("is-open");
        return;
    }
    
    $(this).toggleClass("is-open");
    $(this).next(".footer").slideToggle(500).next(".toggle").slideToggle(500);
    
    $("html, body").animate({
        scrollTop: $(document).height()
    }, 500);
    
    
    });
    

    and it seems to work perfectly, if I knew more about jQuery I probably would have thought to attempt that sooner. But thank you for your help!