Switching between Jquery Slide up and Slide down animations

Currently, I am dealing with ASP.NET MVC 4 and have a nested list within my razor view structured as follows:

<ul class="nav" id="product-cat-menu">
    <li><a href="/Products/Index?category=2002">Dental <i class="fa fa-plus-square-o rightdown"></i></a>
        <ul>
            <li><a href="/Products/Index?category=2004">Materials <i class="fa fa-plus-square-o rightdown"></i></a>
                <ul>
                    <li><a href="/Products/Index?category=2011">Pulp<i class="fa fa-plus-square-o rightdown"></i></a></li>
                    <li><a href="/Products/Index?category=3011">Etchants <i class="fa fa-plus-square-o rightdown"></i></a>
                </ul>
                <li><a href="/Products/Index?category=2006">Medicines <i class="fa fa-plus-square-o rightdown"></i></a>
                    <ul>
                        <li><a href="/Products/Index?category=2011">Pulp<i class="fa fa-plus-square-o rightdown"></i></a></li>
                        <li><a href="/Products/Index?category=3011">Etchants <i class="fa fa-plus-square-o rightdown"></i></a>
                    </ul>
                </li>
        </ul>
    </li>
    <li><a href="/Products/Index?category=2003">Oral <i class="fa fa-plus-square-o rightdown"></i></a></li>
</ul>

In addition to the HTML structure above, here are the CSS styles used for this list:

#product-cat-menu li {
    cursor: pointer;
    position: relative;
}

#product-cat-menu li .rightdown {
    position: absolute;
    right: 0;
}

#product-cat-menu ul {
    display: none;
}

#product-cat-menu li ul li {
    padding: 5px 0 5px 25px;
    width: 100%;
}

Furthermore, there is a jQuery function implemented as follows:

$(document).ready(function () {
    $("#product-cat-menu a").click(function () {
        $("#product-cat-menu ul").slideUp();
        if (!$(this).next().is(":visible")) {
            $(this).next().slideDown();
        }
    });
});

However, upon clicking on the list item, it slides down to reveal the child list, but once redirected to the controller action, it then slides back up. The desired behavior is for the slide down effect to persist even after refreshing the page.

To address this issue, I have attempted the following jQuery code snippet:

$(document).ready(function () {
    $("#product-cat-menu a .rightdown").click(function() {
        //...........
    });
});

Unfortunately, this solution does not seem to be effective in achieving the intended outcome.

Answer №1

To solve the issue with the html anchor tag, you should prevent its default behavior from occurring.

The key missing line of code is:

evt.preventDefault();

Take a look at the complete code example below:

$(document).ready(function () {
    $("#product-cat-menu a").click(function (evt) {
        evt.preventDefault();
        $("#product-cat-menu ul").slideUp();
        if (!$(this).next().is(":visible")) {
            $(this).next().slideDown();
        }
    });
});

You can view this in action on JSFiddle by clicking on this link: DEMO

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

Retrieve vuex state in a distinct axios template js file

I have encountered an issue with my Vue project. I am using Vuex to manage the state and making axios requests. To handle the axios requests, I created a separate file with a predefined header setup like this: import axios from 'axios' import st ...

What are the steps to enable the tab button feature?

Instead of using the ul and li tags, I have implemented three tabs with buttons. My goal is to automatically make the first button active or change its color to green. Despite using the active class in bootstrap, it does not seem to work as expected. How ...

Angular Material Select without underline not specified

I'm currently utilizing Angular Material for my project. My task involves creating a custom component that includes a md-select component. However, I'm facing an issue where the md-select component needs to have no underline, similar to a workaro ...

Troubleshooting problems with dropdown binding in Knockout ObservableArray

I am encountering an issue with fetching data from a restful wcf service during page load and binding it to a dropdown, which is not functioning as expected. function CreateItem(name, value) { var self = this; self.itemNam ...

Troubleshooting the lack of communication between multiple Subscribers in RxJS/Angular when sharing one observable

For a project using Angular 4, I am implementing an HTTP call to retrieve data from the backend. To optimize performance, I cache the data once it is fetched so that subsequent requests do not require additional API calls. getApplication(): Observable< ...

Implementing time and date formatting in Highcharts

I'm almost finished with my highchart, but I am struggling to make the datetime/timestamp display correctly. I'm not sure if it's an issue with the format for the xAxis or the series. https://i.sstatic.net/br0Xn.png Here is my data: [{"x": ...

Attaching a Stylesheet (CSS) without being inside the HEAD Tag

After seeing numerous inquiries about this topic, I have yet to find the answer I seek. My main question is whether it is feasible to connect an external stylesheet in a location other than the HEAD tag. I am facing this issue because I need to use Conflu ...

How can the threejs Box3().getSize() method be effectively utilized?

There seems to be a discrepancy in the proper usage of the Box3() getSize() method, as discussed on various platforms including stackoverflow. const geometry = new THREE.BoxGeometry( 10, 10, 10 ); const material = new THREE.MeshBasicMaterial( {color: 0x00f ...

utilizing the identical characteristics of the parent component

In order for the properties in InputGroup.js to be accessible as this.props in lower-level components like TextInput.js, Checkbox.js, I have created a simple component called InputComponent.js. In this component, I assign this.props to this.prpt so that it ...

The Ion-button seems to be malfunctioning

I am interested in using special buttons for my ionic 1 project, specifically the ion-button feature outlined on this page: Ionic Buttons. I attempted to create a Round Button and an Outline + Round Button: <h2 class="sub-header" style="color:#4 ...

AngularJS allowing multiple ngApps on a single page with their own ngRoute configurations, dynamically loaded and initialized

Seeking advice on lazy loading separate ngApps on one page and bootstrapping them using angular.bootstrap, each with its own unique ngRoute definitions to prevent overlap. I have a functioning plunkr example that appears to be working well, but I am unsur ...

Tips for clearing input fields in React.js without using an empty string

Is there a way to reset the input field in this specific scenario? const [username, setUsername] = useState({ username: "", usernameError: "" }); const handleUsername = (inputUsername) => { if (inputUsername.length > ...

Struggling to reset the jscrollpane scroller

When using jscrollpane in my horizontal division to enable scrolling, I encounter an issue when loading data with ajax. The scrollbar doesn't appear until the browser width is changed. To address this, I currently utilize the following method to rein ...

Changing Array Object into a different Array Object (with Angular)

I am dealing with an array Object [ { product: "Product A", inStock: 3, onHold: 1, soldOut: 2 }, { product: "Product B", inStock: 2, onHold: 0, soldOut: 1 }] I am struggling to convert it into the new array format below. Any assista ...

How to preselect a value in a select tag in Angular 8

I am looking to assign a default value to a select tag, with the values being declared in a table within my component and displayed using the ngFor directive: <form> <div class="form-group col-4"> <span class="badge badge-theme m ...

Combining the container class and my custom wrapper in Bootstrap 3: A step-by-step guide

Is there a way to utilize my own fixed size wrapper in conjunction with a responsive container class from Bootstrap without losing the responsiveness? Here is an example of my current code: <div class="container"> <div class="wrapper"> ...

What is the most effective method for displaying error messages in Extjs?

I am currently using the store.synch() method to post data, with validation being done on the server side. I am currently displaying error messages in a message box, but I want to explore alternative ways to show errors without having to use markInvalid( ...

Tips on saving php variable content in HTML "id"

Three variables are used in PHP: message_id, message_title, and message_content. Their content is stored inside HTML 'id' for later use with jQuery. Example: Variables: $id_variable = $rows['id_mensagem']; $message_title_edit = $rows ...

Notify when a value in a PHP database table is modified

Within my setup, two sheets are utilized. The control sheet showcases a list of job details along with a column that mirrors the notes linked to the active job. On the other hand, the second sheet allows operators to attach new notes. The primary task at ...

The HTML/JS output displays a table with missing horizontal borders

Why aren't my horizontal borders showing up in the output section? Take a look at the code and screenshot below: I want to include horizontal borders and prevent the date fields from moving to the row below. https://i.sstatic.net/qO1KC.png I would li ...