Interactive jQuery Dropdown Menu with Multiple Divs

I have been working on this and I'm almost there, but the jQuery part is driving me crazy (I'm not very proficient in it yet, but I am learning).

Here's the link to the fiddle: http://jsfiddle.net/5CRA5/4/

Since I can't demonstrate the drop down from the dynamically generated post query, I added a search button below the operations and settings buttons that you can click instead of having the dropdown menu.

Everything is functioning properly except for one issue - when someone has the settings menu open and then clicks on operations, the new menu doesn't replace the existing one as expected. I've double-checked the code but it's not behaving as intended. There must be something I am overlooking. All other menus are working fine where they get replaced correctly.

Additionally, I am wondering if there is a way to delay the post action until the div is completely up, so that when it comes back down, the transition appears seamless. Currently, sometimes you can see the information being replaced before the div is fully visible.

Below is the jQuery code for those who may want to take a look at it directly to help identify what might be going wrong.

        function menuCall(div_id, bottom, filename) {
            $('[id^="menu_"]').animate({"top": "-50px"}, "slow");
            $('[id^="menu_"]').delay(500).queue( function(next){$(this).hide();next();});

            $.post("/opserv/" + filename, function(ajaxresult){
                $('#'+div_id).html(ajaxresult);
            });

            if ($('#'+div_id).css('top') !== bottom + 'px') {
                $('#'+div_id).queue( function(next){$(this).show();next();});
                $('#'+div_id).animate({"top": "+" + bottom + "px"}, "slow");
            } else {
                $('#'+div_id).animate({"top": "-50px"}, "slow");
                $('#'+div_id).delay(500).queue( function(next){$(this).hide();next();});
            }
        }

Answer №1

This issue is likely occurring because both the Operations and Settings buttons are toggling the same div - menu_panel-2. When either button is pressed, it causes the div to toggle, displaying or hiding the menu. The solution is to differentiate between which button was pressed, whether it be Settings or Operations. One way to achieve this is by adding a new variable called "type" to the function. Then, you can check if the type is 'Ops' for operations or 'Set' for settings. Based on the type, you can determine whether to show or hide the div. Keep in mind that once you open the Operations or Settings menu, it cannot be hidden unless you switch to another section like search and then return back. To keep track of the last button pressed, save it to a variable and compare it with the 'type'.

Check out this demonstration for reference.

var check = '';
function menuCall(div_id, bottom, filename, type) {
    $('[id^="menu_"]').animate({"top": "-50px"}, "slow");
    $('[id^="menu_"]').delay(500).queue(function(next){ $(this).hide(); next(); });

    $.post("/opserv/" + filename, function(ajaxresult){
        $('#'+div_id).html(ajaxresult);
    });

    if ($('#'+div_id).css('top') != bottom + 'px') {
        $('#'+div_id).queue(function(next){ $(this).show(); next(); });
        $('#'+div_id).animate({"top": "+" + bottom + "px"}, "slow");
    } else {
        if (type == 'Ops' || type == 'Set') {
            if (check === type) {
                $('#'+div_id).animate({"top": "-50px"}, "slow");
                $('#'+div_id).delay(500).queue(function(next){ $(this).hide(); next(); }); 
            } else {
                $('#'+div_id).queue(function(next){ $(this).show(); next(); });
                $('#'+div_id).animate({"top": "+" + bottom + "px"}, "slow");
            } 
        } else {
            $('#'+div_id).animate({"top": "-50px"}, "slow");
            $('#'+div_id).delay(500).queue(function(next){ $(this).hide(); next(); });
        }
    }
    check = type;
}

I hope this explanation clarifies the issue for you.

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 PHP plugin I created seems to be adding an unnecessary whitespace at the end of its output

New to the world of PHP, I challenged myself to create a simple PHP plugin that generates a text greeting based on the time of day for users. To use my plugin, simply drop the .php file into your 'includes' folder and insert a line where you want ...

What could possibly be causing my code to execute multiple times?

Within my code, I have the following function that is triggered when the document is ready: $('#file_upload_form').submit(function(){ // show loader [optional line] //if(document.getElementById('upload_frame') == ...

What is the purpose of using overflow:hidden; in our code?

I am curious about the rationale behind using these three properties on this menu. I don't quite understand why they are necessary. I am eager to delve into the technical reasoning behind this. margin: 0; padding: 0; overflow: hidden; Snippet of HTM ...

Incorporate tinyMCE into a colorbox module

Currently, I am facing an issue on my website where there are textareas with tinymce. While I can view all the textareas correctly, when trying to open colorbox within a textarea, it does not inherit the tinymce properties. The code snippet used to open co ...

Tips for using a JavaScript function to navigate to a specific division (<div>) on an HTML page

I am facing an issue where I need to redirect within the same HTML page that includes a add-form div. What I want is that when I click on a button, my redirection should be to a specific div containing some code. Currently, I have code that redirects to a ...

Is there a way to adjust the height pixel value in my code so it can be dynamic?

I have created a simple script that allows selected objects to fade in as the user scrolls down. However, my issue is that this script is quite rigid. If I were to apply it to 20 different objects, for example, I would need to manually adjust the height ea ...

Problem with special characters in Localstorage for Internet Explorer 8

After developing a jQuery script that retrieves data from a service, stores it in local storage, and displays it on the page upon a specific event, I encountered an issue with displaying Chinese characters only on IE8+ browser. Unfortunately, IE is unabl ...

Issue with Internet Explorer 7 causing table to display with added height

Currently investigating why there is additional space being added by IE. The only fixed data is the width of the image (171 px). Using Jquery, I checked the height of the div in Firefox, Chrome, and Opera which came out to 164px, but in IE 7 it's 172 ...

SASS causing conflicts with CSS breakpoints selector overrides

I am working with a partial file named _display.scss. It includes various @mixin and styling classes related to the display CSS property. _display.scss @mixin d-block{ display: block; } @mixin d-none{ display: none; } .d-block{ @include d- ...

Node.js is struggling to parse JSON data, resulting in undefined values appearing in the console

Issue with parsing JSON data in a node.js environment resulting in 'undefined' display in console. Below is the HTML code snippet: var jsonObjects = [{id:1, name:"amit"}]; jQuery.ajax({ url: 'http://localhost:8081', type: ...

What could be the reason for the fluctuating HTML output?

Exploring the world of CSS and HTML, I noticed a curious change in the output when text is added inside a div tag. First: div.bar { display: inline-block; width: 20px; height: 75px; /* We'll override height later */ background-co ...

Adjusting the selected state of an HTML/CSS checkbox with JavaScript

After downloading a theme with a tailored switch component that replaces the standard checkbox functionality, I noticed that the 'checked' status of the underlying checkbox does not change upon click or touch events. Here is the HTML structure: ...

sequential organization paired with double-column format

Looking for a CSS-only method to arrange an ordered list (ol) into two columns if it exceeds the height of its container. The number of items in the list can vary, and so can the height of the container. When attempting to set li with properties like widt ...

Having Trouble with the Bootstrap Responsive Menu Button Not Working

I'm struggling with the Bootstrap menu I created, as it's not collapsing or animating. Everything seems to be working fine except for the button that isn't functioning. <nav class="navbar navbar-expand-lg navbar-light bg-dark alb ...

Having trouble with the DataTables jQuery plugin? Seeing a blank page instead of the expected results?

I've been trying to set up the Datatables jquery plugin for my HTML table but it's not working as expected. I have linked to the Datatables CDN for CSS styling and script, along with Google's hosted jQuery plugin. Additionally, I have a loca ...

Triggering an Angular AJAX call by selecting a radio button

I am currently working on implementing a CRUD functionality in my app for the admin console. My tech stack includes MongoDB, Spring, Bootstrap, and Angular. The interface consists of a list of radio buttons on the left side containing names of collections ...

React - Although the array in the state is being updated, only the first object in the array is visible on the screen

I'm currently working on developing a web application using the PokeAPI. My primary objective is to retrieve and display a variety of Pokemon based on their type. At the moment, I've set my API URL to specifically fetch fire type Pokemon. Howeve ...

Retrieve AJAX images but experiencing issues with jQuery click function

In the index HTML file, I have 7 images. I have implemented a jQuery function that gets the ids of the images when they are clicked. The jQuery function looks like this: $(".imgss").click(function() { alert($(this).attr("id")); }); I assign the clas ...

Optimizing font size and placement with media queries

I am looking to scale some text and position it over an image on mobile devices. <style> #rcontainer { height: 340px; width: 100%; position: relative; } .rtext span { position: absolute; bottom: 130px; font-family: "Geo ...

What is the best way to assign a unique hour between 9am and 5pm to each column representing an hour?

Currently, the columns are only displaying 9:00am for visual purposes. for (let i = 0; i < 8; i++) { var row = $('<div class="row">'); var col1 = $('<div class="col-md-2"><p class="hour">9:00AM</p>'); v ...