jQuery navigation is experiencing issues due to conflicting buttons

I am currently developing a web application that features two buttons in the header: Share and Options. When a button is clicked, its related content expands and collapses when clicked again. The issue arises when the user clicks on the share button and then proceeds to click the options button without closing the share content, causing the options content to display below the share content.

My goal is to only show the content for one button at a time. If a user clicks on a button while the other content is still open, it should collapse before displaying the new content.

While I am not very experienced with jQuery, I am unsure of how to address this problem.

If you have any suggestions on optimizing my code, I would greatly appreciate your advice. Thank you.


$('.pull_box_share_triger').click(function() {
    if (!collapsed) {  
        var h = document.getElementById('share_box').Height;
        $('#share_box').animate({ height : h+'px' });
        $('#share_link_box').css({ display:'inline-block'});
        $(".overlay").css({ display:'block'});
        $(".pull_box_share_triger").css({ background:'#ffffff' });
        $('#share_social_box').css({ display:'inline-block'});
        $('.share_button').css({ display : 'inline-block'});
    } else {
        $('#share_box').animate({height: 'auto'});
        $('#share_link_box').css({ display:'none'});
        $(".overlay").css({ display : 'none'});
        $(".pull_box_share_triger").css({ background:'#f2f2f2'});
        $('#share_social_box').css({ display:'none'});
        $('.share_button').css({ display : 'none' });
    }
    collapsed = !collapsed;
});

$('.pull_box_option_triger').click(function() {
    if (!collapsed) { 
        var h = document.getElementById('options_box').Height;
        $('#options_box').animate({ height : h+'px' });
        $(".overlay").css({ display:'block'});
        $(".pull_box_option_triger").css({ background:'#ffffff' });
        $(".options_content").css({ display:'inline-block'});
    } else {
        $('#options_box').animate({height: 'auto'});
        $(".overlay").css({ display : 'none'});
        $(".pull_box_option_triger").css({ background:'#f2f2f2'});
        $(".options_content").css({ display:'none'});
    }
    collapsed = !collapsed;
});

Answer №1

Separate two functions are created to hide the share and option content, and they are called upon when buttons are clicked to hide them initially.

$('.pull_box_share_triger').click(function() {
        hideOption();
        if(!collapsed){  
            var h = document.getElementById('share_box').Height;
            $('#share_box').animate({ height : h+'px' });
            $('#share_link_box').css({ display:'inline-block'});
            $(".overlay").css({ display:'block'});
            $(".pull_box_share_triger").css({ background:'#ffffff' });
            $('#share_social_box').css({ display:'inline-block'});
            $('.share_button').css({ display : 'inline-block'});
        } else {
             hideShare();
        }
        collapsed = !collapsed;
    });

    $('.pull_box_option_triger').click(function() {
hideShare();
        if(!collapsed){ 
            var h = document.getElementById('options_box').Height;
            $('#options_box').animate({ height : h+'px' });
            $(".overlay").css({ display:'block'});
            $(".pull_box_option_triger").css({ background:'#ffffff' });
            $(".options_content").css({ display:'inline-block'});
        } else {
               hideOption();
            }
        collapsed = !collapsed;
    });

   function hideShare()
{
  $('#share_box').animate({height: 'auto'});
            $('#share_link_box').css({ display:'none'});
            $(".overlay").css({ display : 'none'});
            $(".pull_box_share_triger").css({ background:'#f2f2f2'});
            $('#share_social_box').css({ display:'none'});
            $('.share_button').css({ display : 'none' });

}

function hideOption()
{
 $('#options_box').animate({height: 'auto'});
            $(".overlay").css({ display : 'none'});
            $(".pull_box_option_triger").css({ background:'#f2f2f2'});
            $(".options_content").css({ display:'none'});

}

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 value of the comment box with the ID $(CommentBoxId) is not being captured

When a user enters data in the comment box and clicks the corresponding submit button, I am successfully passing id, CompanyId, WorkId, and CommentBoxId to the code behind to update the record. However, I am encountering an issue as I also want to pass the ...

Tips for transmitting dropdown selections to a different PHP form using jQuery

After spending a considerable amount of time searching for answers online, I realized that I need some help. Despite finding helpful articles and resources on jQuery, I am still struggling to understand certain concepts. As a beginner in this field, I am w ...

Showcasing a variety of product titles with the help of CSS

Since the HTML is hosted on another server and I have no control over it, how can I use CSS to style certain list items? For example, The HTML on the server side: <ol> <li>Coffee</li> <li>Milk</li> <li>Orange Juice< ...

Inject dynamic HTML to various elements on an AngularJS form and incorporate it into the scope when the button is clicked

Below is an example of a form: <h4>Now let’s break it down. Tell us about the users.</h4> <div id="user1"> <p class="bold">User #1</p> <label>User type:</label> ...

I noticed that my CSS style sheet is only working on three of my pages. What should I do to make sure it affects the fourth page as well?

I am currently facing an issue with my website where I have linked one .css stylesheet to 3 different pages. This is just a temporary template until I finalize the individual look of each page. After performing some div positioning on the site, most pages ...

Scrollmagic - Creating dynamic effects by animating body and div backgrounds using multiple tweens

I'm currently developing a website that utilizes scrollmagic to smoothly transition the color of the active div (each set to size of the screen) from black back to white as you scroll down. The issue I am facing is that the body background color does ...

JavaScript: How can I remove it when I click anywhere on the webpage with onClick?

A challenge I'm facing involves creating a form with an input field that changes its border color when clicked. My goal is for the border to return to its original color when clicking anywhere else on the page. -HTML <form action=""> ...

Working with radio buttons in a loop using PHP

I am facing an issue with a loop that iterates 3 times. In the loop, there's an HTML form containing radio buttons and I am processing the input using PHP. However, when echoing the form data, it does not display the correct values. Is there something ...

What is the best way to trigger a jQuery function when an option is selected from a Select2 dropdown menu?

I have implemented Select2 for custom dropdown styling. The options are structured like this: <option value="001,100">Option 001</option> <option value="002,200">Option 002</option> <option value="003,300">Option 003</opti ...

What is the best way to attach a value to an attribute of a web component in Angular?

In all honesty, I am quite new to Angular and still learning the ropes. Currently, I am faced with a debugging challenge related to one of the users encountering an issue with my web component library. Within this library, there is a custom element create ...

Do Scss mixins have the ability to adapt to different screen sizes

In my CSS, I set different font sizes for different devices: For example: @media only screen and (min-width: 480px) and (max-width: 599px) { t-heading { font-size:14px; } } @media only screen and (min-width: 600px) { t-heading { font-size:24px; } } I ...

Can you explain the distinction between div and span tags?

HTML includes two main tags, div and span. I am curious about the specific functions and roles of each tag. I have experimented with both but haven't been able to identify any significant differences. Can someone provide a detailed explanation on the ...

Execute a zoom out action by pressing the (Ctrl) and (-) keys simultaneously in Javascript

I'm trying to figure out how to simulate a Ctrl - zoom out using Javascript. I've noticed that using the style zoom property or the transform property gives different results with white space in the corners, rather than the smooth zoom out effect ...

Discovering error messages in Django can be made easier through the

Hey there, I'm currently working on a Django website that utilizes jQuery for its AJAX calls. The issue I'm facing is that when one of the AJAX calls fails with a '500 ERROR' from Django, the Response header indicates 'text/plain& ...

How can you easily center content vertically on a web page?

There has been plenty of back-and-forth regarding this issue, with proposed solutions ranging from pure CSS to pure HTML. Some can get quite complex, involving deeply nested divs and intricate CSS rules. I'm looking for a simple and uncomplicated answ ...

Altering the PHP text hue

Is it possible to customize the color of the print and echo text on your HTML page, rather than having it just in black? Thank you. ...

Is the JQuery Mobile .page() method triggering an endless loop?

Creating a dynamic listview with data from an AJAX response has been successful, however, when using JQM's .page() function on it, it appears to enter an infinite loop where the listview keeps getting appended indefinitely. I'm unsure if this is ...

Extract information from a database table for presentation as simple text

I am looking to extract information from each row and display it as plain text on the same page within a paragraph. Here is an example table for reference: <table> <thead> <tr> <th class="a header">A</th ...

Having trouble resolving 'primeng/components/utils/ObjectUtils'?

I recently upgraded my project from Angular 4 to Angular 6 and everything was running smoothly on localhost. However, during the AOT-build process, I encountered the following error: ERROR in ./aot/app/home/accountant/customercost-form.component.ngfactory. ...

What is causing this get method to return such a message?

One of my functions tabulates user-specific data using a GET method that sends a query parameter userId: <script> let thisArray = [] let = userId = $('#userId').val(); $.ajax({ method:&ap ...