.displaying a grid of div elements is not functioning as expected

On a single page, there is a grid of Divs (2 by 3) with a menu (<li> in <lu>) positioned to the left of the grid. Each menu section contains its own grid of divs, all enclosed within one main div to facilitate manipulation.

Menu Sections:

<div id="sections">
        <ul>
            <li class="section1">Product</li>
            <li class="section2">Arcade Sticks</li>
            <li class="section3">Mobility</li>
            <li class="section4">Others</li>
            <li class="section5">Infography</li>
        </ul>
</div>

-

Sample Grids for Projects:

    <div id="grid1">
        <a class="project">Project 1</a>
        <a class="project">Project 2</a>
        <a class="project">Project 3</a>
        <a class="project">Project 4</a>
        <a class="project">Project 5</a>
        <a class="project">Project 6</a>
    </div>
    <div id="grid2">
        <a class="project" style="background-color:#330033;">Project 1</a>
        <a class="project" style="background-color:#330033;">Project 2</a>
        <a class="project" style="background-color:#330033;">Project 3</a>
        <a class="project" style="background-color:#330033;">Project 4</a>
        <a class="project" style="background-color:#330033;">Project 5</a>
        <a class="project" style="background-color:#330033;">Project 6</a>
    </div>
    <div id="grid3">
        <a class="project" style="background-color:#82b8d0;">Project 1</a>
        <a class="project" style="background-color:#82b8d0;">Project 2</a>
        <a class="project" style="background-color:#82b8d0;">Project 3</a>
        <a class="project" style="background-color:#82b8d0;">Project 4</a>
        <a class="project" style="background-color:#82b8d0;">Project 5</a>
        <a class="project" style="background-color:#82b8d0;">Project 6</a>
    </div>

Upon page load, only the grid corresponding to the first menu section (#grid1) is shown while the rest are hidden using jQuery's .hide function.

Below is a snippet of my jQuery script:

$("#project_grid div:not(#grid1)").hide();

for(var i = 1; i<=5; i++){

$(".section"+i).on('click', function()
{
    $("#content #project_grid div:not(#grid"+i).hide();
    $("#content #project_grid #project_large").hide();
    $("#content #project_grid #grid"+i).show();
});
}

I utilize an "i" variable to dynamically retrieve the number following the "section" class value and apply it to the "grid" value within the jQuery script.

The div #project_large represents another group of larger divs showcasing project details that pop up upon clicking on a thumbnail image within the grid. I'm yet to finalize the code for this feature :)


Initially, the grid related to the first menu section is visible whereas others are hidden. However, when I click on a section name in the menu, the "display:none" styling is added but the existing "display: none" is not removed from its counterpart.

As a novice in coding, some areas may seem flawed to you :)

Thank you!

Answer №1

If you want to update your HTML classes, consider using the following format:

    <ul>
        <li class="category" data-grid="grid1">Technology</li>
        <li class="category" data-grid="grid2">Gaming Consoles</li>
        <li class="category" data-grid="grid3">Fitness Trackers</li>
        <li class="category" data-grid="grid4">Smart Home Devices</li>
        <li class="category" data-grid="grid5">Audio Equipment</li>
    </ul>

<div id="grid1" class="grid">
    <a class="item">Item 1</a>
    <a class="item">Item 2</a>
    <a class="item">Item 3</a>
    <a class="item">Item 4</a>
    <a class="item">Item 5</a>
    <a class="item">Item 6</a>
</div>
<div id="grid2" class="grid">
    <a class="item" style="background-color:#330033;">Item 1</a>
    <a class="item" style="background-color:#330033;">Item 2</a>
    <a class="item" style="background-color:#330033;">Item 3</a>
    <a class="item" style="background-color:#330033;">Item 4</a>
    <a class="item" style="background-color:#330033;">Item 5</a>
    <a class="item" style="background-color:#330033;">Item 6</a>
</div>

You can then implement a simple jQuery function like this:

$('.category').click(function() {
    $('.grid').hide();
    $('#' + $(this).data('grid')).show();
});

Check out the example here.

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

Measuring the quantity of 'table' elements with jQuery

This HTML code below defines a table structure. Now tables can be dynamically added to this structure. <div class="tableStyle myWebsiteTable"> <table cellspacing="0" cellpadding="0" id="site0" class="site active"> <thead> ...

Looking for a reliable CSS aggregator for .Net that can merge and minimize style sheets effectively?

Is there an open source or free project that offers a CSS manager? I want to optimize performance and am hoping to find a pre-built solution rather than starting from scratch. Some key features I'm looking for include: Merging multiple .css files in ...

Transferring form data through AJAX for uploading files

My current task involves uploading an image using form data with ajax. I have successfully tested the code below and it is saving the image on my local machine. <form ref='uploadForm' id='uploadForm' action='/tab10/uploadImage& ...

The TypeScript error reads: "An element is implicitly assigned the 'any' type because an expression of type 'any' cannot be used to index a specific type."

[Hey there!][1] Encountering this TypeScript error message: { "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ 0: { image: string; title: string; text: string; }; 1: { ...

Arranging HTML elements using JavaScript or jQuery

Something seems off. The sorting doesn't work as expected. Check out the JavaScript code below: function sortDescending(a, b) { var date1 = $(a).data('date'); var date2 = $(b).data('date'); return date1 > date2; } ...

Exceeded maximum file size event in Dropzone

I am currently implementing dropzone in my form to allow users to upload images. In the event that a user selects a file that exceeds the configured limit, I want to display an alert message and remove the file. Below is my current configuration: Dropzone ...

Displaying Selected DropdownList Value from Database into TextBox in MVC3

Within my MVC3 view, I have a dropdown list that triggers an action when an item is selected, displaying corresponding rows based on the value. The values retrieved need to be shown in a text box within the same view. I've attempted the following: ...

Jquery fails to activate a targeted collapse with radio buttons

Hey there, I'm currently utilizing Laravel Blade to dynamically generate radio buttons and forms based on user selections. Once the user chooses a certain number of jobs, forms will be generated for each job selected. These forms are initially hidden ...

Update the database with the new values and display them in an HTML table

I am working with a table that looks like the one below: <script> $("#edit").hide(); // Initially hide the edit table $("#update").click(function() { $("#edit").toggle(); $("#shown").toggle(); ...

Is there a way to programmatically emulate Firebug's inspect element feature using JavaScript?

Is there a straightforward method in JavaScript or any popular libraries like YUI or jQuery to allow users to interact with elements on a website, similar to the functionality of Firebug for developers? ...

Reduce the line length for better readability

Looking for a way to make a button with flexible width while allowing text to wrap to 2 lines for minimum button size? Check out the examples below: To keep the button width small, even short words should wrap to 2 lines: My Button If there's a ...

Move the accordion slider to the top of the browser

I'm working with an accordion menu that has some long content. To make it more user-friendly, I want to add a slide effect when the accordion items are opened. Right now, if you open the first two menu items, the last item's content is cut off a ...

Enhanced form validation using AJAX

Currently, my aim is to implement client-side validation with AJAX in order to check for any empty fields within a basic form. If a field happens to be blank, I intend to alert the user about its invalidity. It is crucial that the form does not get submitt ...

Rails 3.1 experiencing issues with JQuery loading

I'm having trouble including my jQuery and other JavaScript files in my views. My Gemfile includes: gem 'jquery-rails', '>= 1.0.12' I am using Rails version 3.1.0 In the header section, I have: <head> <%= stylesh ...

Restart simplemodal

In relation to this jQuery plugin found at , I am facing a situation with my ajax function where it takes some time to retrieve the data - therefore, I would like to display a spinner in the modal div. $("#basic-modal-content").modal(); $("#basic-modal- ...

Tips for building nested columns within React Bootstrap Table

I have been working with react bootstrap table, you can find more information here You can check out the code in action by clicking here My goal was to create a table that resembles the one shown below: https://i.sstatic.net/jsZKe.png Here is an example ...

Child div height (with embedded iframe) - issue with responsiveness

I have been working on a simple code for a day now, trying to solve a problem with no progress :( My goal is to display an iframe with a background image. However, the snippet code I have is not showing the background image as intended. You can view the li ...

Developing an animated feature that displays a dynamic count up to the current size of the browser window

I have a script that's able to determine the height and width of my browser window. However, I am facing a challenge in creating a way for these dimensions to count up from zero to their current values upon loading. The desired functionality would be ...

Arrange components within the form

Just started experimenting with this a few days ago. In my form, there is a text field and a button that I want to align side by side at the center of the form. .row { width: 100%; margin-bottom: 20px; display: flex; flex-wrap: wrap; } .col-6 { ...

Displaying a progress bar while uploading images in PHP: A step-by-step guide

I have written the following code: $(function(){ var btnUpload=$('#browse'); var adinfoid=$('#adinfoid').val(); new AjaxUpload(btnUpload, { action: '<?php echo base_url()?>index.php/post/upload_editmainimage/'+adin ...