Show the div only if a different ID is activated

I seem to be having trouble targeting only one div element.

Specifically, when I click on "impressum," only the impressum content is displayed within the <div class="ContentBox">. However, when I click on "AboutMe," both the AboutMe and impressum content are shown.

Am I missing something obvious?

This is what I currently have:


        <div id="menu">
        <div class="content">
            <ul>
                <li><a href="#"><img src="img/menu/home.png">Home</a></li>
                <li><a href="#" class="ContentBox_open" id="AboutMe"><img src="img/menu/ich.png">About Me</a></li>
                <li><a href="#" class="ContentBox_open" id="impressum"><img src="img/menu/impressum.png">Impressum</a></li> 
            </ul>
        </div>
    </div>

    <div class="ContentBox">
        <div class="close"><img id="ContentBox_close" src="img/menu/close.png" alt="close"></div>
        <div id="impressumTxt" style="display:none;">
            <h1>Impressum</h1>
            <p>Lorem ipsum dolor sit amet</p>
        </div>
        <div id="AboutMeTxt" style="display:none;">
            <h1>About Me</h1>
            <p>Lorem ipsum dolor sit amet</p>
        </div>
    </div>
    <script type="text/javascript" charset="utf-8">
        $(function(){
            // show impressum
            $('.ContentBox_open').click(
                function(event) {
                    var ContentId = event.target.id;
                    $('#'+ContentId+'Txt').fadeTo( 10, 0.90 );
                    $('.ContentBox').fadeTo( "slow", 0.90 );
                }
            );

            // hide impressum
            $('#ContentBox_close').click(
                function() {
                    $('.ContentBox').fadeTo( 1000, 0 );
                }
            );
        });
    </script>

Thank you for your assistance!

Answer №1

To ensure child content divs are hidden when the container is closed, you must hide them before fading in the parent container again. It's important to assign a common class to items with similar behavior for easier access (such as class="content"):

For example:

$('.ContentBox').fadeTo(1000, 0, function(){
    $(this).find('.content').hide()
});

Check out this JSFiddle for reference: http://jsfiddle.net/zo83qpdq/

$(function () {
    // Show impressum
    $('.ContentBox_open').click(function (event) {
        var ContentId = event.target.id;
        $('#' + ContentId + 'Txt').fadeTo(10, 0.90);
        $('.ContentBox').fadeTo("slow", 0.90);
    });

    // Hide impressum
    $('#ContentBox_close').click(function () {
        $('.ContentBox').fadeTo(1000, 0, function(){
            $(this).find('.content').hide()
        });
    });
});

If you only want one div to be displayed at a time, you need to modify the logic to hide all other content divs.

For example, check out this version on JSFiddle: http://jsfiddle.net/zo83qpdq/1/

Answer №2

To conceal the Textsection of About Me and Impressum before revealing another section, simply assign a class to them.

In this scenario, an open section is not closed before opening another.

Just include $('.class').hide() before unveiling the new section

Example on Fiddel

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

Is Django_compressor concealing the CSS code?

I have implemented django_compressor in my project. This is how I set it up in my template : {% load compress %} {% compress css %} <link rel="stylesheet" href="/media/css/master.css" type="text/css" charset="utf-8"> {% endcompress %} In my setti ...

Data retrieval seems to be encountering issues in Firefox and IE9, whereas Chrome and Safari are functioning without any problems

I am using the following method function callCommentservice() { try { // Comment Service Url var getCommentServiceUrl = self.commentsServiceUrl + self.getRating + "tenantId=" + self.tenantId + "&ratedObjectTypeId=" + sel ...

Increase the size of the textarea when it is clicked on and revert back to its original size when it

My question revolves around a text area that I am trying to manipulate through jQuery. The desired functionality is to increase the height of the text area whenever someone clicks on it, and decrease the height when clicking anywhere else on the screen. & ...

Tips for maintaining a fixed size for CSS grid cells' minimum and maximum dimensions

I am attempting to utilize CSS grid to create an entirely fixed layout. This layout is for an embedded application that contains both GUI elements and text fields. The sizes of the cells should remain unchanged and not readjust as their content grows or sh ...

The Express middleware is not being utilized

There seems to be an issue with the middleware in the code below that is supposed to create a property req.user if req.session.user exists. Unfortunately, it appears that the middleware is not being triggered, causing 'hihihhiihi' to not be print ...

Emphasizing specific terms by changing them (but only within p tags)

After spending about 8 hours searching for a solution yesterday on various platforms, I almost gave up. But then I realized I should turn to this community for some much-needed help. So here is my first question - a big thank you to everyone who has helped ...

Using NodeJS to handle server side FormData

Currently, in the process of building a web application with nodejs on the server-side, I am facing a challenge of transferring PDF files from the client to the server. Client side: var files = new FormData(); var count = 0; $('#tableSlideId tr&apos ...

Initial value preselected for radio button

Is there a way to ensure that the Default radio button selected in the following code is always "Period"? Currently, when the page loads, none of the radio buttons are selected. <tr> <th valign="top" align="left"> Scope ...

What steps can I take to allow a third-party JavaScript to access my TypeScript object?

I am working on an Angular application and I have a requirement to develop an API for a third-party JavaScript that will be injected dynamically. public class MyApi{ public callSomeFunction():void{...} public getSomeValue():any {...} } var publicA ...

How to Retrieve a Targeted Element from a Multidimensional Array

I've got some data that looks like this: [[12, 23],[27,-6],[52, -32],[82, 11]] How can I access specific elements within these arrays? With a standard array like this: [a, b, c, d] It's easy to reference 'b' as arrayName[1]. But ho ...

Fill in a URL using the information provided in the boxes and navigate to the website

My colleagues and I access the following link multiple times a day, clicking through several pages to reach this specific URL: http://eharita.mamak.bel.tr/imararsiv/test.aspx?f_ada=36391&f_parsel=4 I am looking to create an HTML page with two input b ...

Utilizing Ajax data for CSS manipulation to display a live preview populated with form inputs

I am faced with a challenge involving a form containing approximately 80 input fields of type text and select. Each input field pertains to CSS values, and I aim to display a preview showcasing the new values (onChange any input value) in the form without ...

Next.js Configuration Files Explained

Currently, my application is functioning well with the use of Next.js and Next.js API tool for handling requests. The backend is managed through sanity.io which is working smoothly. In my sanity configuration setup, I have a dedicated file named 'san ...

Tips on sharing global variables in Express 4.*

If I have a variable called data = {} stored in a file named data.js; When I import this file into another page, I only receive the value of the variable. How can I obtain its reference instead? By reference, I mean that if I make any changes in the impo ...

Using a debugger with jQuery callback functions for optimal performance

Here is the jQuery function I am using: function logIn() { debugger; $.ajax({ type: "POST", url: '@Url.Action("LogIn", "Login")', success: function (result) { debugger; if (result == "Ok") ...

CSS navigation menu with drop-down functionality

I am currently learning CSS and HTML as I work on the CCTCcart project. When running the code in the bottom menu section, I noticed that when clicking on any menu item such as Products, the white background color merges with blue. I need to find a solution ...

Developing pledges in AngularJS

I am currently working on implementing a promise in Angular using the $q service to retrieve an object from a web service. The unique aspect of this implementation is that if the object is already cached, it should be returned without making a call to the ...

What is the best way to send a message to only one specific client using socket.io instead of broadcasting to everyone?

After thoroughly researching the documentation, I am still unable to find a solution. My goal is to send data to a specific client or user instead of broadcasting it to everyone. I have come across other inquiries regarding this issue, but most either rem ...

Updating default values in reactive() functions in Vue 3: A step-by-step guide

Currently, I am in the process of developing an application using Nuxt 3 and implementing the Composition API for handling async data. The specific scenario I am facing is this: I have a page that displays articles fetched from the database using useLazyFe ...

Issues arise with jQuery ajax calls when targeting checkboxes in IE, whereas the functionality runs smoothly on Chrome and Firefox

When using the following ajax call triggered by a checkbox, Internet Explorer fails to work properly, while it works fine in Firefox and Chrome. <input type="checkbox" name="16086" id="16086" value="1" onclick="jQuery.ajax({type:'POST',dataTy ...