Numerous links were chosen and multiple div elements were displayed on the screen

Currently, I have a setup where selecting one div will show its content. However, I am looking to enhance this by allowing multiple divs to be displayed simultaneously. For example, if 'Div 1' is selected and shown, I want the content of 'Div 2' to appear when it's clicked without hiding 'Div 1'. Can anyone provide assistance with achieving this?

HTML

<div class="btn-group" role="group" aria-label="">
  <a id="showAll" class="btn btn-primary">Show All Elements</a>
  <a class="showSingle btn btn-primary" data-target="1">Div 1</a>
  <a class="showSingle btn btn-primary" data-target="2">Div 2</a>
  <a class="showSingle btn btn-primary" data-target="3">Div 3</a>
  <a class="showSingle btn btn-primary" data-target="4">Div 4</a>
</div>

<div id="div1" class="targetDiv">Lorum Ipsum1</div>
<div id="div2" class="targetDiv">Lorum Ipsum2</div>
<div id="div3" class="targetDiv">Lorum Ipsum3</div>
<div id="div4" class="targetDiv">Lorum Ipsum4</div>

jQuery

$('.showSingle').click(function() {
    $('.targetDiv').hide();

    $('#div' + $(this).attr('data-target')).show();
    $('.btn').removeClass('active');
    $(this).addClass('active');
});

Example on jsFiddle

Answer №1

If you're looking for a straightforward toggle function, here's how I achieved it by adapting your example:

JS

$('#showAll').click(function() {
    $('.btn').addClass('active');
    $('.targetDiv').show();
    $(this).addClass('active');
});

$('.showSingle').click(function() {
    $('#div' + $(this).attr('data-target')).toggle();
    $(this).toggleClass('active');
});

Markup

<div class="btn-group" role="group" aria-label="">
  <a id="showAll" class="btn btn-primary active">All Items</a>
  <a class="showSingle btn btn-primary active" data-target="1">Div 1</a>
  <a class="showSingle btn btn-primary active" data-target="2">Div 2</a>
  <a class="showSingle btn btn-primary active" data-target="3">Div 3</a>
  <a class="showSingle btn btn-primary active" data-target="4">Div 4</a>
</div>

<div id="div1" class="targetDiv">Lorem Ipsum1</div>
<div id="div2" class="targetDiv">Lorem Ipsum2</div>
<div id="div3" class="targetDiv">Lorem Ipsum3</div>
<div id="div4" class="targetDiv">Lorem Ipsum4</div>

Fiddle: https://jsfiddle.net/hescano/42uwvhx4/2/

Take note of the usage of .toggleClass() and .toggle() to achieve the desired functionality.

Answer №2

Perhaps this solution will get the job done:

$('#showAll').click(function() {
$('.btn').removeClass('active');
$('.targetDiv').show();
$(this).addClass('active');
    });

    $('.showSingle').click(function() {
  $('.targetDiv').hide();


  var targetHeight = $(this).attr('data-target');

  $(".targetDiv").each(function(){
    var height = $(this).attr('data-value');
    if(height <= targetHeight){
      $(this).show();
    }


  });

  $('.btn').removeClass('active');
  $(this).addClass('active');
    });

You could consider updating the HTML for the target divs like this instead of parsing the id values:

<div id="div1" class="targetDiv" data-value="1">Lorum Ipsum1</div>
<div id="div2" class="targetDiv" data-value="2">Lorum Ipsum2</div>
<div id="div3" class="targetDiv" data-value="3">Lorum Ipsum3</div>
<div id="div4" class="targetDiv" data-value="4">Lorum Ipsum4</div>

Answer №3

To effectively hide and show elements on click, one method is to utilize an additional class that toggles the visibility of elements. By removing this class on click, only the selected element will be revealed while the others remain hidden. This prevents all items from being hidden when calling $('.hideClass').hide():

.hideClass{ display: hidden; }

$('.showSingle').click(function() {
    $('#div' + $(this).attr('data-target')).removeClass("hideClass");
    $('.hideClass').hide();
    ...
});

Demo

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

Tips for reducing code length in jQuery

Here's an interesting question that I've been pondering. Is there a more efficient way to optimize and condense code like the one below? Could it be achieved using functions or loops instead of having lengthy jQuery code? var panels = ["panel1", ...

What is the best way to make the current year the default selection in my Select control within Reactive Forms?

Hey there! I managed to create a select element that displays the current year, 5 years from the past, and 3 years from the future. But now I need to figure out how to make the current year the default selection using Reactive Forms. Any ideas on how to ac ...

Show a webpage depending on specific JavaScript criteria

I have a condition in my JavaScript code that determines whether or not a user should be granted access to a specific page. However, I don't want users to be able to directly access this page even if they know the URL. This page contains both HTML and ...

What could be the reason behind the failure of this :after element?

I am facing an issue with the preloader on my webpage where the animation is not displaying as expected. The animation should appear on top of a dark black background before the page fully loads, but it seems to be missing. The CSS for the animation works ...

What is the best way to set a specific maximum size for the dropdown selector in Material UI's TextField component?

Working on a Sign up Page involves prompting users to select their location from a dropdown list of different countries. However, the issue arises when the dropdown list covers the entire screen, making it difficult for users to navigate. Is there a way to ...

When the page is refreshed, reorienting axes in three.js encounters difficulties

I am currently working on a project that involves using the three.js editor source code available for download on the three.js website. As part of this project, I am required to adjust the orientation of the axes to align with standard airplane coordinate ...

Retrieve CSS height using Jquery, based on the declared value rather than the computed value

In a previous version of jQuery, the default behavior was different. When I use .css("height") and .height(), it gives me the computed height instead of what I actually want. I only need the height value if it is explicitly declared in the CSS for that ele ...

Set up a mouseover function for a <datalist> tag

Currently, I am delving into the world of javascript/jquery and I have set up an input field with a datalist. However, I am encountering a slight hurdle - I am unable to trigger an event when hovering over the datalist once it appears. Although I have man ...

Transfer information using the Ajax jQuery technique

I would greatly appreciate your assistance. Can you please help me with one more thing? I have the code below and I am having trouble figuring out where I am making a mistake: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.j ...

Establishing the REM value for all CSS properties

My goal is to implement the use of rem units throughout my website. However, relying on the standard rem-to-px conversion rate doesn't feel very intuitive: 10px = 0.625rem 12px = 0.75rem 14px = 0.875rem 16px = 1rem (base) 18px = 1.125rem 20px = 1.25r ...

Is it possible to utilize X-Y coordinates as repositories for values beyond just X-Y coordinates themselves?

I am in the process of creating a tile-based grid and I need to expand the X-Y coordinates to include additional values for determining characteristics of each tile, such as resources (for example, food, water, stone, lumber) within a game context. Conside ...

Utilize Foundation Framework to format JSON output with custom styles for rows, columns, and

Brand new to this, so please bear with me! I've managed to get my json file and output working, but now I need assistance styling it the way I want using the Foundation Framework. For example, let's say my json file looks something like this: ...

What is the best method to obtain the following id for an image?

Whenever I hover over the li with the number 3, I am getting the wrong number for the next id in the img tag. The first time, I get next id = 4 which is incorrect. But on the second hover, I get next id = 0 which is what I actually need. How can I correctl ...

What is preventing me from accessing the sub-elements of an AngularJS controller?

I know this question has probably been asked countless times before, but I'm struggling to find the right words to explain my specific issue. Essentially, I'm having trouble accessing something like $ctrl.thing.subelement. However, the following ...

Stop unwanted clicking on inactive buttons in Angular

I want to make sure that disabled buttons cannot be clicked by users who might try to remove the disabled attribute and trigger an action. Currently, I have this code to handle the situation: <button [disabled]="someCondition" (click)="executeAction()" ...

No instances are returned by Processing.instances

I am experiencing an issue with a webpage that is running 2 processing sketches. I came across a suggestion to call Processing.instances[0].exit() in response to a question on dynamically unloading a Processing JS sketch from canvas. However, when I attem ...

Listen for the modified value in a directive with AngularJS

I am currently working on implementing a directive that can draw a chart based on specified values. What I am aiming for is to pass the data necessary for the plot directly from the template rather than using ng-model, as my current solution requires. I ...

Choosing a particular svg path in d3.js using JSON attribute

Is it feasible to isolate just one of the SVG paths generated from a geoJSON file using D3? In my scenario, the paths represent different areas on a map. Users can pick an area from a dropdown menu. I aim to utilize the selected value from this list to pin ...

Enhancing Symfony's performance through optimized Ajax response time

When using Symfony2, I am experiencing differences in loading times for AJAX requests between development and production environments. In development, it takes 1 second to load, while in production it only takes 500 milliseconds for a simple call: Here is ...

Avoiding clashes between CSS styles in web applications

As I work on developing a web application that can be embedded on external websites, one of the challenges I am facing involves constructing dialogues with their own unique stylesheets. This could potentially lead to conflicts if both my dialogue container ...