Reputation:
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
Reputation: 253506
From what you describe, I'd suggest:
$('.content').filter(function () {
return $(this).css('display') === 'block';
}).prev('h3').find('img.button').addClass('rotate');
References:
Upvotes: 3