user1320260
user1320260

Reputation:

jQuery - 'if' a class has CSS then assign class to a sibling

I have slideToggle set to 3 sections. I rotate an arrow button image on click. I have set it so that 2 of the sections are visible initially. I would like to 'auto' assign the button within these sections a rotate class so then the button is pre-roatetd on the div's that are already visible. I know I can do this by assigning the class manually, but is there a way of doing it automatically with jQuery?

DEMO JSFIDDLE

Currently I have this set up

$('.button').click(function(){
    $(this).toggleClass('rotate');
    $(this).parent().next().slideToggle();
});

I was wondering if I could do something like this at the top of the script (the code below does not work, but it demonstrates the logic of what I'm trying to achieve)

if($('.content').css('display') == "block"){
    $(this).parent().next('.button').toggleClass('rotate');
}

Upvotes: 0

Views: 85

Answers (1)

David Thomas
David Thomas

Reputation: 253506

From what you describe, I'd suggest:

$('.content').filter(function () {
    return $(this).css('display') === 'block';
}).prev('h3').find('img.button').addClass('rotate');

JS Fiddle demo.

References:

Upvotes: 3

Related Questions