Guide to displaying list-group elements in container 2 after being selected from container 1 in Bootstrap 4

Using bootstrap 4, I have two list-groups in separate containers placed side by side. My goal is to display items in the second container when a list item from the first container is selected. How can I achieve this?

The number of rows in the right container should determine the number of items displayed in the second container. We can use placeholder values for now.

<div class="col-6">
  <div class="card ">
      <div class="card-header py-2">Group Name</div>
      <div class="list-group">
          <a href="#" class="list-group-item py-0 list-group-item-action">Container Left 7 items</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Cras justo odio 10 items</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Dapibus ac facilisis 17 items</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Morbi leo risus 12 items</a>                            
      </div>
  </div>
</div>
<div class="col-6">
  <div class="card">
      <div class="card-header py-2">Items</div>
      <div class="list-group">
          <a href="#" class="list-group-item py-0 list-group-item-action">Container Right</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Cras justo odio</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Dapibus ac facilisis in</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Morbi leo risus</a>                            
      </div>
  </div><!--class="card"-->
</div><!--class="col-6"-->

Here's my example on Plunker

I'd appreciate any assistance with implementing a vertical scroll bar after exceeding 9 items, keeping the selection highlighted in blue, and ensuring that Container 2 updates when an item from Container 1 is chosen.

Answer №1

Here is a straightforward solution to your problem, using simple Jquery code and some minor HTML adjustments. Keep in mind that there are multiple ways to achieve the same result.

In this implementation, I have utilized basic Jquery code along with slight modifications to the HTML you provided in Plunker. Feel free to customize it further based on your requirements.

To specify container selection, you can include a data-set attribute in each anchor element within both containers. The value of data-set in the left container indicates the number of items expected in the right container. Similarly, the value of data-set in the right container denotes the total items available.

Below is the HTML structure:

<div class="col-6">
  <div class="card ">
    <div class="card-header py-2">Container Left</div>
    <div id="leftContainer" class="list-group">
      <a href="#" data-set="2" class="list-group-item py-0 list-group-item-action">Container Left 7 items</a>
      <a href="#" data-set="4" class="list-group-item py-0 list-group-item-action">Cras justo odio 10 items</a>
      <a href="#" data-set="3" class="list-group-item py-0 list-group-item-action">Dapibus ac facilisis 17 items</a>
      <a href="#" data-set="5" class="list-group-item py-0 list-group-item-action">Morbi leo risus 12 items</a>                            
    </div>
  </div>
</div>
<div class="col-6">
  <div class="card">
    <div class="card-header py-2">Container Right</div>
    <div id="rightContainer" class="list-group" style="height:225px; overflow-y: scroll">
      <a href="#" data-set="1" class="list-group-item py-0 list-group-item-action">1</a>
      <a href="#" data-set="2" class="list-group-item py-0 list-group-item-action">2</a>
      <a href="#" data-set="3" class="list-group-item py-0 list-group-item-action">3</a>
      <a href="#" data-set="4" class="list-group-item py-0 list-group-item-action">4</a>                            
      <a href="#" data-set="5" class="list-group-item py-0 list-group-item-action">5</a>
      <a href="#" data-set="6" class="list-group-item py-0 list-group-item-action">6</a>
      <a href="#" data-set="7" class="list-group-item py-0 list-group-item-action">7</a>
      <a href="#" data-set="8" class="list-group-item py-0 list-group-item-action">8</a>
      <a href="#" data-set="9" class="list-group-item py-0 list-group-item-action">9</a>
      <a href="#" data-set="10" class="list-group-item py-0 list-group-item-action">10</a>
      <a href="#" data-set="11" class="list-group-item py-0 list-group-item-action">11</a>
      <a href="#" data-set="12" class="list-group-item py-0 list-group-item-action">12</a>
      <a href="#" data-set="13" class="list-group-item py-0 list-group-item-action">13</a>
      <a href="#" data-set="14" class="list-group-item py-0 list-group-item-action">14</a>
    </div>
  </div><!--class="card"-->
</div><!--class="col-6"-->

Make sure to notice the styling of the rightContainer div, which addresses the vertical scroll issue (mentioned in the second part of your query). You may need to adjust the height for individual items once the styling is complete.

Below is the Jquery function responsible for handling the selection process:

$("#leftContainer > a").click(function(event){
  event.preventDefault();
  $("#leftContainer > a").removeClass("active");
  $(this).addClass("active");
  var leftDataSet = parseInt($(this).attr("data-set"));
  $("#rightContainer > a").removeClass("active");
  $("#rightContainer > a").hide();
  $("#rightContainer > a").each(function(){
    if(leftDataSet >= parseInt($(this).attr("data-set"))){
      $(this).show();
    }
  });
});

I have used the active Bootstrap class to highlight selected elements. For highlighting selections as per the third part of your inquiry, you can utilize the following snippet:

$("#rightContainer > a").click(function(event){
  event.preventDefault();
  $(this).toggleClass("active");
});

To address the final segment of your question, you can initially hide all anchors by either adding a d-none class to them in the HTML or executing this line at the beginning of your JavaScript file before the above events:

$("#rightContainer > a").hide();

I hope this explanation proves useful!

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

Can you explain the functionality of this: http://welbog.homeip.net/ ?

Can you explain how this operates?: All you need to do is scroll down. The design is impressive, absolutely impressive, and it functions seamlessly without the use of javascript. How is that possible? ...

How to set a specific text color when hovering over a link with jQuery

Seeking assistance in customizing a hover effect for spans within links. The current code applies the effect to all links with spans, rather than just the hovered item. Any tips on refining the code to target specific elements would be highly valued. $( ...

Show the information received from ajax on the webpage

I am facing an issue with displaying the value I receive from AJAX data in this modal. I have set the id="view_name" on a span element but for some reason, it's not showing up. Below is the code snippet that triggers the fun_view function: <td> ...

Tips for displaying the webpage background above a specific div by utilizing a differently shaped div

I designed a webpage with a background image. At the top, I placed a div element (let's call it div_1) set to 50% width which creates a horizontal black bar with 60% opacity. Directly above that, towards the left at 50%, I want another div element (di ...

Conceal the .dropdown-backdrop from bootstrap using solely CSS styling techniques

Is there a way to hide the .dropdown-backdrop element from Bootstrap for a specific dropdown on a webpage using only CSS? I found a solution that involves Javascript, you can view it on JSFiddle here. However, I am hoping to achieve this without relying o ...

Issue with Clicking on Table Rows in JQuery (Specifically in Internet Explorer)

The script functions perfectly in Firefox and Opera browsers. $(document).ready(function() { $('#mainTable tr').each(function() { $(this).on("click",( function () { ...

The images are positioned within the middle of the page, directly beneath the sidebar, rather than at the top. (Vue.Js)

I'm currently working on a Vue.JS photo website and I've hit a roadblock as described in the title. Despite trying various solutions, after spending two hours back and forth with ChatGPT, I've decided to seek help here. Below is the code sn ...

When the session times out, Ajax mistakenly displays the login page instead of an error message

I have a question that is somewhat similar to this one, but I will explain my understanding of the situation and the ideas I have come up with to fix it. Hopefully, someone can guide me in the right direction. The scenario: In my webapp, an authenticated ...

JQuery drag and drop functionality experiencing issues in Chrome Browser specifically when integrated into a Joomla Article

I recently embedded a jQuery drag and drop example into my Joomla article. Surprisingly, it works perfectly fine on Firefox browser but encounters issues on Chrome. Although the drag and drop functionality works on Chrome, the problem is that the buttons b ...

What is the best way to create horizontal lines that span multiple Bootstrap columns?

I am working with a Bootstrap grid that consists of three columns in a row. My goal is to add a line across all columns under every row of content written in the columns. <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.boo ...

Changing the background color using ReactJS

After streamlining my project to focus on the main issue, I am still facing a problem. Here is the relevant code snippet: .App{ background-color: aqua; } .AboutMe{ color:blue; background-color: yellow; } This is from App.js: import logo from '. ...

Holding off on routing the pathName through the router until the next-page-transitions have been completed

In my current setup, I have two distinct elements at play. Using the code snippet below: className={router.pathname.startsWith("/pagename") ? "menu-active" : ""} I am able to apply the menu-active class to the pagename naviga ...

Modifying the charCode value associated with the pressed key

In the code snippet below, I am trying to modify the charCode of the pressed key, but it doesn't seem to work. My intention is to have the letter "a" output as "b" when pressed. Can anyone spot what I am doing incorrectly? $("#target").keypress(funct ...

Issue with JQuery: object.id is returning as undefined even though it should have a value

While working with JQuery, I encountered a rather peculiar (or possibly foolish) error. The HTML code snippet in question is as follows: <input type="password" name="repeatPassword" id="id_repeatPassword" /> And within my javascript code, I have t ...

Limiting the options available in dropdown menus

In my HTML code, I have two drop-down lists that look like this: <select name="from" id="abs-from-1"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> ...

Rotating SVG graphics with a growth effect

Check out my CSS below: /* -------------------------- Base --------------------------- */ body { padding-top: 60px; background: #0f4e7a; } svg { margin: auto; display: block; width: 28%; } .star{ fill: #FFF; } /* ------------------ ...

Experiencing stack overflow issue while implementing a basic ajax chat functionality with JQuery and ASP.NET

I am currently working on developing a basic ajax chat system using jQuery and ASP.NET. The functionality of my code is as follows: Upon page load, the 'chatbox' div is refreshed with a request to the messages.aspx page, which retrieves new me ...

Storing Tags with jQuery Tag-it: A guide to saving user inputted tags in an object

Recently stumbled upon a fantastic plugin called tagit, and now I'm looking for a way to extract tag values from it and convert them into an object. Any assistance on this matter would be greatly welcomed! ...

Tips for extracting data from various select drop-down menus within a single webpage

As a newcomer to JQuery, I apologize if this question seems basic. I have a page with 20 categories, each offering a selection of products in a drop-down menu. The user will choose a product from each category, triggering an ajax call to retrieve the pric ...

Implementing a time delay in the jQuery keyup() function following an Ajax request

I am currently facing a complex issue and I am uncertain about the best approach to tackle it. I have multiple textboxes lined up in a row that need to be filled in. Each time a value is entered into a textbox, I make an Ajax call to process that value. De ...