How can you determine the status of the display element affected by .slideToggle()?

I have attempted to use is(':visible'), is(':hidden'), and css('display') but I am unable to retrieve the display status of an element after applying .slideToggle()

What could I be overlooking?

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

and here is the code sample:

<!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>

Answer №1

Make sure to wait for the completion of slideToggle before checking the 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)
    );

Answer №2

After hearing about the idea of "event finishing" from gp., I was able to discover this thread.

jQuery wait for event to complete

I had no idea that I didn't know about this concept and how it was relevant to the issue I faced. Thank you for the insight.

Check out 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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The $routeProvider is unable to load the view specified in ngRoute

I am currently in the process of implementing ngRoute into my application, but I am facing challenges with getting it to function properly. To showcase my efforts, I have developed a basic app. index.html: <!DOCTYPE html> <html ng-app="tester"& ...

Express: using async and await leads to an error when attempting to access "result" before it has been initialized

When using the login function that utilizes ExecuteSQL to validate user existence, an error occurs during file execution related to async await. ReferenceError: Cannot access 'result' before initialization at /Users/naseefali/Documents/Projects ...

What is the process of importing a file as text into vite.config.js?

Within my project directory, there is a simple SCSS file that I need to access its content during the compilation process in order to transform it in vite.config.js. However, I am unsure of how to retrieve its content. I have successfully obtained the cont ...

What is the best way to relocate a child component to a different parent in React?

I have a list of child components with checkboxes, and when a checkbox is clicked, I want to move that child component inside another div. Below is an illustration of how my app should look. I need to select student names and shift them up under the "Pres ...

The script generated by document.write is failing to function as expected

A completed JQuery script has been created to enable the movement of a 360° object (picture) based on mouse actions. The code is functional: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> ...

Looking to expand the width of the b-modal component in a Vue.js application using Bootstrap

Using bootstrap v4 for VueJS, I encountered a challenge with increasing the width of b-modal. Despite trying various solutions mentioned here, nothing seemed to work. <b-modal :ref="fieldName" :id="fieldName" :title="msg" size="lg" modal-class="b-modal ...

Steps to extract viewmodel information from a specific controller following an ajax request

I am encountering an issue with passing updated data from my controller to the view after making an Ajax call. Here is a simplified version of what I am trying to achieve: Javascript $ANALYZE = $('#submitID'); $ANALYZE.click(function () { ...

Using an imported material icon as a background in JSS styling

I have implemented a draggable dialog component in React-mui with a resizable box feature. const styles = theme => ({ resizable: { position: "relative", "& .react-resizable-handle": { position: "absolute" ...

The onChange function failed to provide the expected target value

I attempted to implement an onChange function for a TextArea component, but I encountered an issue where the state was not updating when using console.log or passing it to the payload. Interestingly, when I used JSON.stringify, the value was successfully ...

Navigating jQuery Tabs by Linking to a Specific Tab in a Separate File

I am currently using Bootstrap 3 to develop a basic website. On the about.html page, I have set up Tabs with various content. <ul class="nav nav-tabs" id="TabSomos"> <li class="active"><a href="#somos" data-toggle="tab">About Us</a> ...

A guide on interpreting the details of the stripe webhook response

I have successfully set up a ColdFusion file to intercept the response from the STRIPE webhook. <cfsavecontent variable="headerdump"> <cfdump var="#toString(getHttpRequestData().content, 'utf-8')#" expand="yes" format="text"> < ...

Steps for utilizing the Bootstrap filter to search for a specific name starting with a particular letter

I have implemented a bootstrap filter for searching, but I am encountering an issue. Currently, when I type 'n', it displays all names containing 'n' like Nathan and Arjan. However, I would like it to only show names that start with &ap ...

Getting JSON data from PHP in AngularJS involves making an HTTP request to the PHP file

I've been searching extensively, but haven't found a solution. I need the data from my PHP script to be formatted like this: $scope.places = [{name: 'John'},{name: 'Jane'}]; My issue is figuring out how to make this happen. ...

Is there a way to enable popovers globally and also utilize the container: 'body' option simultaneously?

My Bootstrap 5 popovers seem to be missing the arrow and I suspect it's because of interference from the parent element. The documentation provides a solution by using the container: 'body' option on a single item. How can I apply this to al ...

Struggling with setting margins effectively in Bootstrap for the desired layout

I am attempting to create two columns with space between them, but whenever I try to add a margin-right, one of the columns goes down. Can someone please assist me? <div class=row> <div class="boxi col-6"> Content here ...

Transferring a file between server APIs using Node.js (Express)

I have a situation where I need to make a client-side API request that sends a POST request to my express server with a formData file. This file then needs to be accessed in the server route and forwarded to a third party API. const addAttachment = async ...

What is the best way to fetch data when a single page is in view?

I recently created a jQuery mobile website where I incorporated D3 to visualize some data. The site consists of multiple pages, each defined by <div data-role="page" id="...">...</div>. Given the large amount of data that needs to be loaded f ...

Leverage Vue's ability to inject content from one component to another is a

I am currently customizing my admin dashboard (Core-UI) to suit my specific needs. Within this customization, I have an "aside" component where I aim to load MonitorAside.vue whenever the page switches to the Monitor section (done using vue-router). Here ...

Responsive 4-Column CSS Footer Design

I am currently working on developing a simple 4 column CSS footer that adapts to various screen resolutions such as desktop, tablet, and mobile devices. The footer should have a maximum width of 980 pixels, with the blue background expanding to fill the w ...

Select the element to emphasize it and reduce the visibility of others

I'm currently facing an issue with a block that contains multiple divs representing products. My goal is to have the selected product highlighted while the rest appear less prominent when clicked. I've managed to achieve this functionality, howev ...