0

I've tried is(':visible'), is(':hidden') and css('display') but I can not get the display status of an element after it has been through .slideToggle()

What am I missing?

Here is the jsfiddle: http://jsfiddle.net/djlerman/jbNg3/1/

and the code:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test Slide Toggle</title>

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" />

<style type="text/css">
.ui-icon {
    float:left;
}
</style>

<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">
$(document).ready(function () {
    $('span#expandList').click(function () {
        if ($(this).attr('class') == "ui-icon ui-icon-triangle-1-s") {
            $(this).attr('class', "ui-icon ui-icon-triangle-1-e");
        } else {
            $(this).attr('class', "ui-icon ui-icon-triangle-1-s");
        }

        $(this.parentNode).nextAll('ul').eq(0).slideToggle();

        $('#debug').html($(this.parentNode).nextAll('ul').eq(0).attr('id') +  ' is(:visible): ' +  $(this.parentNode).nextAll('ul').eq(0).is(':visible') + '<br />' +
                        $(this.parentNode).nextAll('ul').eq(0).attr('id') + ' is(:hidden): ' + $(this.parentNode).nextAll('ul').eq(0).is(':hidden') + '<br />' +
                        $(this.parentNode).nextAll('ul').eq(0).attr('id') + ' .css(display): ' + $(this.parentNode).nextAll('ul').eq(0).css('display') + '<br />'
                        );

    })

});
</script>
</head>
<body >
<h3>
  <span id="expandList" class="ui-icon ui-icon-triangle-1-s" ></span>
  Heading 1
</h3>
<ul id="heading1">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>


<h3>
  <span id="expandList" class="ui-icon ui-icon-triangle-1-s"></span>Heading 2
</h3>
<ul id="heading2">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>


<h3>
  <span id="expandList" class="ui-icon ui-icon-triangle-1-s"></span>Heading 3
</h3>
<ul id="heading3">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>
<div id="debug"></div>
</body>
</html>
j08691
  • 204,283
  • 31
  • 260
  • 272
Donavon Lerman
  • 343
  • 1
  • 6
  • 19

2 Answers2

0

wait for slideToggle to finish before checking status.

    $(this.parentNode).nextAll('ul').eq(0).slideToggle(
        (function () {

            $('#debug').html($(this.parentNode).nextAll('ul').eq(0).attr('id') +  ' is(:visible): ' +  $(this.parentNode).nextAll('ul').eq(0).is(':visible') + '<br />' +
                        $(this.parentNode).nextAll('ul').eq(0).attr('id') + ' is(:hidden): ' + $(this.parentNode).nextAll('ul').eq(0).is(':hidden') + '<br />' +
                        $(this.parentNode).nextAll('ul').eq(0).attr('id') + ' .css(display): ' + $(this.parentNode).nextAll('ul').eq(0).css('display') + '<br />'
                        );

        }).bind(this)
    );
gp.
  • 8,074
  • 3
  • 38
  • 39
  • This worked. Thank You. Is there another way to wait for the slideToggle to finish? Here is the updated jsFiddle: http://jsfiddle.net/djlerman/jbNg3/3/ – Donavon Lerman Oct 08 '13 at 18:01
  • What does the ".bind(this)" do? – Donavon Lerman Oct 08 '13 at 20:54
  • when the function is executed, "this" would refer to whatever context the function was invoked on. To make it invoke on your "span#expandList" object which "this" refers to outside the function, bind is used. It's equivalent to jQuery.proxy http://api.jquery.com/jQuery.proxy/ – gp. Oct 09 '13 at 01:25
0

Once gp. mentioned the concept of the "event finishing" I was able to find this thread.

jQuery wait for event to complete

I didn't know that I didn't know about this concept and how it related to the issue I was having. Thank you for the incite.

Here is the updated jsFiddle: http://jsfiddle.net/djlerman/jbNg3/3/

and the updated code.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test Slide Toggle</title>

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" />

<style type="text/css">
.ui-icon {
    float:left;
}
</style>

<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">
$(document).ready(function () {
    $('span#expandList').click(function () {
        if ($(this).attr('class') == "ui-icon ui-icon-triangle-1-s") {
            $(this).attr('class', "ui-icon ui-icon-triangle-1-e");
        } else {
            $(this).attr('class', "ui-icon ui-icon-triangle-1-s");
        }

        $(this.parentNode).nextAll('ul').eq(0).slideToggle( function () {

            $('#debug').html($(this).attr('id') +  ' is(:visible): ' +  $(this).is(':visible') + '<br />' +
                             $(this).attr('id') + ' is(:hidden): ' + $(this).is(':hidden') + '<br />' +
                             $(this).attr('id') + ' .css(display): ' + $(this).css('display') + '<br />'
                            );
        }).bind(this);

    })

});
</script>
</head>
<body >

<div id="debug"><br /><br /><br /></div>
<hr />

<h3>
  <span id="expandList" class="ui-icon ui-icon-triangle-1-s" ></span>
  Heading 1
</h3>
<ul id="heading1">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>


<h3>
  <span id="expandList" class="ui-icon ui-icon-triangle-1-s"></span>Heading 2
</h3>
<ul id="heading2">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>


<h3>
  <span id="expandList" class="ui-icon ui-icon-triangle-1-s"></span>Heading 3
</h3>
<ul id="heading3">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>

</body>
</html>
Community
  • 1
  • 1
Donavon Lerman
  • 343
  • 1
  • 6
  • 19