Responsive Bootstrap table unable to expand div upon clicking

My bootstrap responsive table has a unique functionality that allows specific divs to expand and collapse upon button click. While this feature works seamlessly in desktop view, it encounters issues on mobile devices.

CSS

.expandClass[aria-expanded=true] .fa-chevron-circle-right {
display: none;
}
.expandClass[aria-expanded=false] .fa-chevron-circle-down  {
display: none;
}

html

<table id="respTableId" class="table table-striped table-bordered table-responsive"  
style="width: 100%;">
<thead>
<tr>
<th style="width:50%;">Column 1</th>
<th style="width:50%">Column2</th>
</tr>
</thead>
<tr>
<td>data1</td>
<td> <a class="expandClass" data-toggle="collapse" href="#collapseId1" role="button" 
aria-expanded="false" aria-controls="collapseId2">
Click to View / Hide 
<i class="fa fa-chevron-circle-right" style="font-size:1.5em;"></i>
<i class="fa  fa-chevron-circle-down" style="font-size:1.5em;"></i>
</a>
<div class="collapse" id="collapseId1">
Test Data 1
</div>
</td>

<td>data2</td>
<td> <a class="expandClass" data-toggle="collapse" href="#collapseId2" role="button" 
aria-expanded="false" aria-controls="collapseId2">
Click to View / Hide 
<i class="fa fa-chevron-circle-right" style="font-size:1.5em;"></i>
<i class="fa  fa-chevron-circle-down" style="font-size:1.5em;"></i>
</a>
<div class="collapse" id="collapseId2">
Test Data 2
</div>
</td>

<td>data3</td>
<td> <a class="expandClass" data-toggle="collapse" href="#collapseId3" role="button" 
aria-expanded="false" aria-controls="collapseId3">
Click to View / Hide 
<i class="fa fa-chevron-circle-right" style="font-size:1.5em;"></i>
<i class="fa  fa-chevron-circle-down" style="font-size:1.5em;"></i>
</a>
<div class="collapse" id="collapseId3">
Test Data 3
</div>
</td>
</tr>
</table>

While the above code functions correctly on desktops, I am facing challenges with its behavior on mobile devices. I have attempted an alternative approach using the following javascript code, but the issue persists.

Javascript

function showHideDiv(id)
{
var x = document.getElementById(id);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}

Answer №1

If you're looking to achieve this without the need for any custom CSS or JavaScript code, you can simply utilize the Bootstrap library.

<!-- 
  Bootstrap docs: https://getbootstrap.com/docs
-->

<div class="container">
  <div class="row">
    <table id="respTableId" class="table table-striped table-bordered table-responsive"  
style="width: 100%;">
<thead>
<tr>
<th style="width:50%;">Column 1</th>
<th style="width:50%">Column2</th>
</tr>
</thead>
<tr>
<td>data1</td>
<td> <a class="expandClass" data-toggle="collapse" href="#collapseId1" role="button" 
aria-expanded="false" aria-controls="collapseId2">
Click to View / Hide 
<i class="fa fa-chevron-circle-right" style="font-size:1.5em;"></i>
<i class="fa  fa-chevron-circle-down" style="font-size:1.5em;"></i>
</a>
<div class="collapse" id="collapseId1">
Test Data 1
</div>
</td>

<td>data2</td>
<td> <a class="expandClass" data-toggle="collapse" href="#collapseId2" role="button" 
aria-expanded="false" aria-controls="collapseId2">
Click to View / Hide 
<i class="fa fa-chevron-circle-right" style="font-size:1.5em;"></i>
<i class="fa  fa-chevron-circle-down" style="font-size:1.5em;"></i>
</a>
<div class="collapse" id="collapseId2">
Test Data 2
</div>
</td>

<td>data3</td>
<td> <a class="expandClass" data-toggle="collapse" href="#collapseId3" role="button" 
aria-expanded="false" aria-controls="collapseId3">
Click to View / Hide 
<i class="fa fa-chevron-circle-right" style="font-size:1.5em;"></i>
<i class="fa  fa-chevron-circle-down" style="font-size:1.5em;"></i>
</a>
<div class="collapse" id="collapseId3">
Test Data 3
</div>
</td>
</tr>
</table>
<table id="respTableId" class="table table-striped table-bordered table-responsive"  
style="width: 100%;">
<thead>
<tr>
<th style="width:50%;">Column 1</th>
<th style="width:50%">Column2</th>
</tr>
</thead>
<tr>
<td>data1</td>
<td> <a class="expandClass" data-toggle="collapse" href="#collapseId1" role="button" 
aria-expanded="false" aria-controls="collapseId2">
Click to View / Hide 
<i class="fa fa-chevron-circle-right" style="font-size:1.5em;"></i>
<i class="fa  fa-chevron-circle-down" style="font-size:1.5em;"></i>
</a>
<div class="collapse" id="collapseId1">
Test Data 1
</div>
</td>

<td>data2</td>
<td> <a class="expandClass" data-toggle="collapse" href="#collapseId2" role="button" 
aria-expanded="false" aria-controls="collapseId2">
Click to View / Hide 
<i class="fa fa-chevron-circle-right" style="font-size:1.5em;"></i>
<i class="fa  fa-chevron-circle-down" style="font-size:1.5em;"></i>
</a>
<div class="collapse" id="collapseId2">
Test Data 2
</div>
</td>

<td>data3</td>
<td> <a class="expandClass" data-toggle="collapse" href="#collapseId3" role="button" 
aria-expanded="false" aria-controls="collapseId3">
Click to View / Hide 
<i class="fa fa-chevron-circle-right" style="font-size:1.5em;"></i>
<i class="fa  fa-chevron-circle-down" style="font-size:1.5em;"></i>
</a>
<div class="collapse" id="collapseId3">
Test Data 3
</div>
</td>
</tr>
</table>
    </div>
</div>  

Check out a live demo here: jsfiddle

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

Passing Data from Child to Parent Components in ReactJS

I'm new to React and JavaScript, currently working on a website. I've encountered an issue with passing data between components from child to parent. Here's the scenario: In my App.js script, I'm using react-router-dom for routing. I ...

What sets v-on:event apart from this.$on(event, handler) in VueJs?

I've been delving into Vue.js event handling and I believe developers can utilize this.$on('event', handler) in their JavaScript files to handle the 'event'. An interesting example demonstrates this concept. <div id="maina ...

Stylish dots emerging under navigation bar

Okay, so I've run into a simple issue, but I can't seem to figure out the solution. Ever since I wrote this code, I've noticed these small dots appearing below the menu. Take a look at the image for a clearer picture. https://i.sstatic.ne ...

CSS/HTML: A guide to creating an element that switches to absolute positioning when scrolled past

Just starting out with CSS and HTML here, trying to figure out how to make an element become absolutely positioned once you scroll past it on the page. Take a look at this example for reference: (no need for an account to test). As you scroll down, the b ...

"The concept of hoisting in JavaScript and its impact on global

Is there a way to set the variable "username_show" from this PouchDB document? I attempted using hoisting, but it seems that I may need to move the global variable outside of the data structure and then outside of the function. var db = new PouchDB ...

Why does the onclick function fail to trigger when placed within the form element?

This form utilizes the react-bootstrap styling library. Strangely, when I place the submit button inside the form tag, it causes the app to break and restart. However, moving the button outside of the form tag resolves the issue. Can someone explain why ...

Utilizing JQuery for parsing information

I am working on a project that involves displaying Google Maps API on one tab and images on another within a container. The goal is to have the image in the second tab change when a location is selected from the list. To achieve this, I have set up a hidd ...

Launch a fresh tab using Chrome's extensions feature

Currently, I am in the process of developing a Google extension that will search for selected text on Google when the user clicks "ctrl+alt+x". Here is the mainfest for the extension: { "name": "A jQuery Chrome extension", "version": "0.1", "descri ...

Searching for various object values within an array and then adding two properties together in JavaScript

I am working with an array of objects that require me to analyze two properties in order to calculate a value. let data = [ { NodeId: "9837f279", NodeName: "Node1", summary: { current: 50, limit: 75 ...

Upgrading to React Router v6: Implementing Loader Functions with Context API

Having issues implementing loaders in React-Router V6 while making a request for a page through a function located in the context file. Unfortunately, I can't access the context from main.js where the router is defined. Main.js import ReactDOM from & ...

Uncovering the secrets to fetching numerous JSON files asynchronously using JavaScript and JQuery

My goal is to fetch multiple JSON files using JavaScript or jQuery and extract elements named j_name, j_value, and j_sts into sarr[i], rarr[i], and parr[i], respectively. var sarr = new Object; var rarr = new Object; var parr = new Object; //num_json rep ...

Issue with Three.js Object3D.applyMatrix() causing inaccurate scaling

Exploring the wonders of Three.js version r.71! I have recently delved into Three.js and have come across an interesting challenge. I am working on streaming geometry and handling position/scale/rotation changes between clients using Socket.io and NodeJS. ...

Adaptable side menu with Tailwind CSS

I've been struggling to create a responsive sidebar using tailwindcss for quite some time. Currently, it appears like this It looks fine on a 1920x1080 screen, but not on larger or smaller screens. When I scale it down, the text becomes too cramped a ...

The CSS property overflow:hidden or overflow:scroll is not functioning as expected when applied to a

On my practice website, I have set up a demonstration for showcasing text data. The issue arises when the user inserts an excessive amount of characters in the text box. To address this, I would like the text to be scrollable so that all content can be d ...

Preventing jQuery plugin from overriding default settings

I have created a jQuery plugin using the nested namespace pattern, inspired by the design template found in Addy Osmani's book. The plugin is a simple gallery and everything seems to be functioning correctly. However, I am facing an issue when attemp ...

How to prevent default validation message in .NET MVC for time inputs

There is an input field with the type time displayed below: <input type="time" name="visible-start-time" id="visible-start-time" class="form-control half-input" value="@(Model==null?"":Model.StartTime.TimeOfDay.ToString())"/> https://i.sstatic.ne ...

Function wrapper intended for axios

How can I create a wrapper function for axios? I am looking to create a function that can return axios, allowing for easy substitution with another fetch API in the future. This is what I have attempted so far: import axios from 'axios' expor ...

What are some solutions for troubleshooting a rapid-moving Selenium web driver?

My code is not functioning correctly and I need a better solution. The fast speed of execution makes it difficult for me to detect where the issue lies. Below is my current code: public static void main(String[] args) { //**************************** ...

Exploring the use of nested routes in react-router version 6

Attempting to comprehend nested routes and unsure if they are set up correctly. The Dashboard component is intended to house all content, with a 404 page displayed if the path does not match. However, when trying to access /about, it consistently redirect ...

Using Functional Programming with Node.js: A guide to waiting for a function to complete

As a newcomer to Node.js, I am satisfied with the syntax of JavaScript as I have utilized it for constructing web interfaces. With substantial experience in Object-Oriented Programming from Java and C#, along with an understanding of functional programming ...