Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if DIV is open or closed after slideToggle

I have a Jquery Ui button ( #toggleIcons) that when clicked, toggles a div (.icons) to show some icons. I am also using Jquery Isotope and Infinitescroll to add new images dynamically. What I am trying to do is to have a way for the slideToggle state to be retained as the new images are added and updated. Ifinitescroll has a callback function so that I can update the page and state of the icons.

//My button click function
$("#styleSwitcher #toggleIcons" ).click(function() {
   $('.icons').slideToggle('slow');
   //for Isotope to update the layout
   $('#container').isotope('reLayout') 
    return false;
});

//code I am working on to put in callback to test if div is open or closed
if($(".icons").is(":hidden"))
{
  $('.icons').hide();
}
else
{
  $('.icons').show();
}

Not working. Any help or direction would be appreciated. thanks

like image 333
Macsupport Avatar asked Mar 10 '11 20:03

Macsupport


4 Answers

You have your condition backwards:

if ($(".icons").is(":visible")) { <-----
  $('.icons').hide(); 
} else {
  $('.icons').show(); 
}
like image 61
Mike Gleason jr Couturier Avatar answered Nov 04 '22 16:11

Mike Gleason jr Couturier


Put check state script in a function - let it hold a bit..

$('.advance-view').click(function() {
    $('#advance-control').slideToggle('slow', function() {
        if ($('#advance-control').is(':hidden'))
        {
            $('.advance-view').css("background-color", "#2B509A");
        }
        else
        {
            $('.advance-view').css("background-color", "#4D6837");
        }
    });             
});
like image 40
Ryan Harne Avatar answered Nov 04 '22 15:11

Ryan Harne


I would use :visible

if($(".icons:visible").length > 0)
    //item is visible
else
    //item is not visible

but if you want to stick to your code

if($(".icons").is(":hidden"))

should probably read

if($(".icons").is(":hidden").length > 0)
like image 2
Dutchie432 Avatar answered Nov 04 '22 16:11

Dutchie432


why not just toggle or slideToggle it?

$(".icons").toggle();
like image 2
FatherStorm Avatar answered Nov 04 '22 17:11

FatherStorm