When a block is clicked, jQuery will reveal that block while hiding the others sequentially, starting with the second block, then the third, and finally the fourth

Creating a navigation menu with 4 blocks can be a bit tricky, especially when trying to show one block at a time upon click. Here is my code attempt, but unfortunately it's not working as expected. I would greatly appreciate any help or suggestions on how to fix it. Thank you! You can view the code below or by visiting this link: http://jsfiddle.net/9afaF/

<div class="games_content">

            <script>
                $(function() {
                    $("#block_2 div,#block_3 div,#block_4 div").hide();

                    $("#block_1 div").click(function(){
                        $("#block_2 div,#block_3 div,#block_4 div").hide();
                        $("#block_2 div."+$(this).attr('id')).show("fast");

                        $("#block_2 div").click(function(){
                            $("#block_3 div,#block_4 div").hide();
                            $("#block_3 div."+$(this).attr('id')).show("fast");

                            $("#block_3 div").click(function(){
                                $("#block_4 div").hide();
                                $("#block_4 div."+$(this).attr('id')).show("fast");
                            })
                        });
                    });
                });
            </script>
            <div style="float:left;width:237px;height:420px;background: rgba(255, 0, 0, .5);margin-right:15px;" id="block_1">
                <div id="category1">Category 1</div>
                <div id="category2">Category 2</div>
                <div id="category3">Category 3</div>
                <div id="category4">Category 4</div>
                <div id="category5">Category 5</div>
            </div>

            <div style="float:left;width:164px;height:500px;background: rgba(255, 0, 0, .5);margin-right:2px;margin-top:10px;margin-left:4px;" id="block_2">
                <div class="category1" id="subcat1">Sub Category 1</div>
                <div class="category1" id="subcat2">Sub Category 2</div>
                <div class="category1" id="subcat3">Sub Category 3</div>
                <div class="category2" id="subcat4">Sub Category 4</div>
                <div class="category2" id="subcat5">Sub Category 5</div>
                <div class="category3" id="subcat6">Sub Category 6</div>
                <div class="category3" id="subcat7">Sub Category 7</div>
                <div class="category4" id="subcat8">Sub Category 8</div>
                <div class="category4" id="subcat9">Sub Category 9</div>
                <div class="category5" id="subcat10">Sub Category 10</div>
                <div class="category5" id="subcat11">Sub Category 11</div>
            </div>

            <div style="float:left;width:160px;height:500px;background: rgba(255, 0, 0, .5);margin-right:2px;margin-top:10px;" id="block_3">
                <div class="subcat1" id="game1">Sub Sub Categoey 1</div>
                <div class="subcat1" id="game2">Sub Sub Categoey 2</div>
                <div class="subcat2" id="game3">Sub Sub Categoey 3</div>
                <div class="subcat2" id="game4">Sub Sub Categoey 4</div>
                <div class="subcat3" id="game5">Sub Sub Categoey 5</div>
                <div class="subcat4" id="game6">Sub Sub Categoey 6</div>
            </div>

            <div style="float:left;width:240px;height:400px;background: rgba(255, 0, 0, .5);margin-left:17px;" id="block_4">
                <div class="game1">Game1</div>
                <div class="game2">Game2</div>
                <div class="game3">Game3</div>
                <div class="game4">Game4</div>
            </div>

Answer №1

It appears that the jquery library is missing from your fiddle.

To see it in action, click on this FIDDLE

$(function() {
    $("#block_2 div,#block_3 div,#block_4 div").hide();

    $("#block_1 div").click(function(){
        $("#block_2 div,#block_3 div,#block_4 div").hide();
        $("#block_2 div."+$(this).attr('id')).show("fast");

        $("#block_2 div").click(function(){
            $("#block_3 div,#block_4 div").hide();
            $("#block_3 div."+$(this).attr('id')).show("fast");

            $("#block_3 div").click(function(){
                $("#block_4 div").hide();
                $("#block_4 div."+$(this).attr('id')).show("fast");
            })
        });
    });
});

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

In Next.js, a peculiar issue arises when getServerSideProps receives a query stringified object that appears as "[Object object]"

Summary: query: { token: '[object Object]' }, params: { token: '[object Object]' } The folder structure of my pages is as follows: +---catalog | | index.tsx | | products.tsx | | | \---[slug] | index.tsx | ...

jQuery is consistently lagging one step behind when calculating the sum with keypress

I am currently working on developing a calculator that adds up the values entered with each key press. Here is the code snippet I have been using: $('#padd').keypress(function() { var ypoints = "200"; var points = parseInt( $(th ...

Guide on transforming an array of objects into a fresh array

I currently have this array: const initialData = [ { day: 1, values: [ { name: 'Roger', score: 90, }, { name: 'Kevin', score: 88, }, { name: 'Steve&apo ...

Uploading a file to a URL using Node.js

Looking for a way to replicate the functionality of wget --post-file=foo.xpi http://localhost:8888/ in nodejs, while ensuring it's compatible across different platforms. In need of assistance to find a simple method for posting a zip file to a specif ...

How can I use jQuery to add styling to my menu options?

Looking to customize my menu items: <ul> <li class="static"> <a class="static menu-item" href="/mySites/AboutUs">About Us</a> </li> <li class="static"> <a class="static-menu-item" href="/m ...

Why isn't Freichat displaying the name of the user who logged in?

After creating my own small social networking site, I decided to add a chat script called freichat. However, I am facing an issue where when a user logs in, their name appears as "Guest102" instead of their actual name. I am quite confused by this problem ...

Tips for handling catch errors in fetch POST requests in React Native

I am facing an issue with handling errors when making a POST request in React Native. I understand that there is a catch block for network connection errors, but how can I handle errors received from the response when the username or password is incorrec ...

Is there a way to store session variables in Angular without the need to make an API call?

I am currently working with a backend in PHP Laravel 5.4, and I am looking for a way to access my session variables in my Angular/Ionic project similar to how I do it in my Blade files using $_SESSION['variable_name']. So far, I have not discove ...

What is the best way to make an HTML table with a static header and a scrollable body?

Is there a way to keep the table header fixed while allowing the table body to scroll in an HTML table? Any advice on how to achieve this would be greatly appreciated. Thanks in advance! ...

What is the procedure for iterating through the square brackets of a JSON array?

Here's the data I have: $json_data_array = '[ { "id": 1, "value": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfd7cdffcbdacccb91dcd0d2">[email protected]</a>", ...

Is it possible to attach React Components to Elements without using JSX?

In my React Component, I have the following code: import React, { useEffect } from 'react' import styles from './_PhotoCollage.module.scss' import PhotoCard from '../PhotoCard' const PhotoCollage = ({ author }) => { let ...

How can I rectify the varying vulnerabilities that arise from npm installation?

After running npm audit, I encountered an error related to Uncontrolled Resource Consumption in firebase. Is there a solution available? The issue can be fixed using `npm audit fix --force`. This will install <a href="/cdn-cgi/l/email-protection" clas ...

Is it possible to utilize Angular translate to support multiple languages for individual components or modules?

Utilizing ngx-translate to switch the language of my application has proven to be quite challenging. My application consists of different languages specifically for testing purposes and is divided into separate modules with lazy loading functionality. As ...

Event handler for the MouseLeave event is not successfully triggering when dealing with anchor

When viewing a link to a Codepen, the issue is most noticeable in Chrome: https://codepen.io/pkroupoderov/pen/jdRowv Sometimes, the mouseLeave event fails to trigger when a user quickly moves the cursor over multiple images, resulting in some images reta ...

Setting the z-index of the parent element causes issues with overlapping

When adjusting the z-index property of the parent element, I am experiencing issues with overlapping elements. Below is the HTML and CSS code: .black_gradient{ width:100%; height:100%; background: linear-gradient(0deg,rgb(75, 75, 75) 20%, rgba(75,75 ...

Polymer event / callback for appending new child nodes

My current project involves creating a custom element, let's call it <parent-element>, that performs specific actions based on the presence of its childNodes. When I define the element like this: <parent-element> <div> </div&g ...

What is the method for tallying CSS page breaks in printed HTML documents?

Currently, for my report generation process using HTML and CSS to manage page breaks dynamically. I've enabled the option for users to print multiple reports at once by combining them into different sections within a single HTML document. This helps p ...

Compose an email without the need to access the website

Is there a way to send email reminders to users without having to load the website pages? In the reminder section, users can select a date for their reminder and an email will be automatically sent to them on that date without requiring them to access the ...

Rearrange items in a display: contents wrapper using CSS Grid

I'm having trouble positioning descendants in a root grid while they are enclosed in a container with display: contents. The issue I am facing is that one element is not being correctly placed within the grid: .wrapper { width: 80ch; margin: a ...

Exploring AngularJS through interactive sessions

I am working on implementing a basic login system using express and angularjs. The angular js application is running on a different server (grunt server localhost:9000), while the express app is running on a separate port. In my express app, I have configu ...