Click the 'expand' button for additional details on every row in the table

I am facing a challenge with my HTML table where I have a lot of information to add for each row. However, displaying everything at once makes the page look cluttered. Therefore, I am looking to add a "view more" button in another column for each row.

Despite trying to write JavaScript code to achieve this, it is not working as intended. My requirements are:

  1. The row should remain invisible until I click the "view more" button (I attempted wrapping the tags inside a div, but it resulted in placing the row outside the table).

  2. I need the code to apply to all rows so that I don't have to create separate codes for each one.

  3. When viewing extended information for a row, I want other extended information to remain hidden.

  4. If possible, I would like a simple animation to show the extended content.

Below is the snippet of my HTML code:

<table>
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Action</th>
    </tr>
    <tr>
        <td>John</td>
        <td>Doe</td>
        <td><a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;" class="btn bN-btn-base bN-btn-small">View More</a></td>
    </tr>
    <tr>
        <td colspan="3">
            <div id="example" class="more">
                <p>John Doe is a man</p>
                <p style="text-align:right;"><a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;">Hide this content.</a></p>
            </div>
        </td>
    </tr>
    <tr>
        <td>Jane</td>
        <td>Doe</td>
        <td><a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;" class="btn bN-btn-base bN-btn-small">View More</a></td>
    </tr>
    <tr>
        <td colspan="3">
            <div id="example" class="more">
                <p>Jane Doe is a woman</p>
                <p style="text-align:right;"><a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;">Hide this content.</a></p>
            </div>
        </td>
    </tr>
</table>

This is how the CSS looks like:

<style type="text/css">
   .more {
      display: none;
   }
   a.showLink, a.hideLink {
      text-decoration: none;
      color: #36f;
      padding-left: 8px;
      background: transparent url(down.gif) no-repeat left; 
   }
   a.hideLink {
      background: transparent url(up.gif) no-repeat left;
   }
   a.showLink:hover, a.hideLink:hover {
      border-bottom: 1px dotted #36f; 
   }
</style>

Finally, here is the JavaScript portion:

<script language="javascript" type="text/javascript">
function showHide(shID) {
   if (document.getElementById(shID)) {
      if (document.getElementById(shID+'-show').style.display != 'none') {
         document.getElementById(shID+'-show').style.display = 'none';
         document.getElementById(shID).style.display = 'block';
      }
      else {
         document.getElementById(shID+'-show').style.display = 'inline';
         document.getElementById(shID).style.display = 'none';
      }
   }
}
</script>

Answer №1

Assuming you are open to using jQuery since you've tagged it, here is a sample fix with jQuery:

UPDATE: The HTML has been modified, causing the second row to fail due to the ID selector

Markup Language (HTML)

<table>
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Action</th>
    </tr>
    <tr>
        <td>John</td>
        <td>Doe</td>
        <td><a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;" class="btn bN-btn-base bN-btn-small">View More</a></td>
    </tr>
    <tr>
        <td colspan="3">
            <div id="example" class="more">
                <p>John Doe is a man</p>
                <p style="text-align:right;"><a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;">Hide this content.</a></p>
            </div>
        </td>
    </tr>
    <tr>
        <td>Jane</td>
        <td>Doe</td>
        <td><a href="#" id="example-show" class="showLink" onclick="showHide('exampleFemale');return false;" class="btn bN-btn-base bN-btn-small">View More</a></td><!-- Note the change in the showHide() function -->
    </tr>
    <tr>
        <td colspan="3">
        <div id="exampleFemale" class="more"><!-- Note the change in the ID -->
                <p>Jane Doe is a woman</p>
                <p style="text-align:right;"><a href="#" id="example-hide" class="hideLink" onclick="showHide('exampleFemale');return false;">Hide this content.</a></p><!-- Note the change in the showHide() function -->
            </div>
        </td>
    </tr>
</table>

JavaScript

function showHide(shID) {
    if ($('#' + shID)) {
      if ($('#' + shID+'-show').css('display') != 'none') {
       $('#' + shID+'-show').css({'display':'none'});
       $('#' + shID).css({'display':'block'});
      }
   }
   else {
       $('#' + shID+'-show').css({'display':'inline'});
       $('#' + shID).css({'display':'none'});
   }
}

Notice the amendments in JavaScript->jQuery where

document.getElementById(shID+'-show')
used to be and now it's $('#' + shID+'-show'), and .style.display = 'none'; changed to .css({'display':'none'});

Remember to add jQuery library

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

Trust this explanation helps!

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

What is the reason behind the necessity for a React class component to always invoke super(props) within its constructor function?

I recently came across a tutorial from reactjs.org that mentioned the importance of calling the base constructor with props in class components. However, further research led me to a StackOverflow answer suggesting that super() can be used instead of super ...

Ways to include text with specific choices in a drop-down menu?

Within a form, I am encountering a situation where a select box's options are pre-selected through ajax based on a previously entered value. I am now seeking a way to append additional text to these pre-selected options only. for (i in data) { $("#my ...

AngularJS and IE8 don't play nice when it comes to handling dynamic content

Below is the code snippet I am working with: <blockquote class='mt20'> <p><span>&ldquo;</span><span>{{iq.quote}}</span><span>&rdquo;</span></p> <footer><cite class="d ...

The child element with a minimum height of 300 pixels is failing to inherit the height of its parent

Here is the HTML code for a div block I am working with: <div className={'row ge-container'}> <div className={'a-span3 ge-container-navigation'}> hello </div> <div className={'a-span9 ge-containe ...

Unable to define an object within the *ngFor loop in Angular

In order to iterate through custom controls, I am using the following code. These controls require certain information such as index and position in the structure, so I am passing a config object to keep everything organized. <div *ngFor="let thing of ...

Query: How can I retrieve the value of a td element in a dynamic table?

Below is an example of how the table will be structured: <tr class="A" id="A${a.index}"> //id will be generic such as 1,2,3,4,5 <table id="mytable"> <tr> <td>a</td> <td>b</td> ...

My goal is to develop a table that is both able to be resized and easily repositioned

I've been working on a project to develop an interactive table that can be manipulated, resized, and repositioned within a canvas. The code snippet below shows my attempt at creating this table on the canvas: var canvas = document.getElementById("dra ...

Include a lower border on the webview that's being shown

Currently, the webview I'm working with has horizontal scrolling similar to a book layout for displaying HTML content. To achieve this effect, I am utilizing the scroll function. My inquiry revolves around implementing a bottom border on each page usi ...

Choose the text that appears in the input or textbox field when tapping or clicking on it

Desperately Seeking a Clickable Textbox The Quest: In search of a cross-browser textbox/input field that can select its content on click or tap. An elusive challenge haunting developers for years. The Dilemma: Using a touch device triggers the tap e ...

PHP's 'include' function is now being ported into modern Javascript as a new method

As the library of JS frameworks continues to expand, I'm wondering if there is a simple JS replacement or alternative for PHP's 'include' function. Is PHP include still a relevant method for including chunks of code, or are there better ...

Issue with 'firebase.auth is not defined' error following SDK update

I've been using the Firebase JS sdk for web development without any issues for a year now. However, after recently updating my code from version 5.3.1 to the latest 6.5.0 SDK version, I encountered the following error: TypeError: firebase.auth is no ...

Manipulating the DOM by nesting an icon within a button element in HTML

Currently working on my todo list project and I have a question about using innerHTML. I'm curious if I can include an icon inside of a button tag like this: btn.innerHTML = '<i class="fas fa-times fa-lg"></i>'; So, wo ...

Two neighboring sections containing dynamic elements. One section automatically adjusts its size to fit the content, while the other allows

I need to display 2 adjacent boxes with dynamic content rendered using Angular. The Container should have the same height as Box1. The height of Box2 may vary due to its dynamic nature, but it should never be taller than Box1. If it does become higher, a s ...

What are the steps for adding JQuery UI to a Blazor WebAssembly project?

I am delving into the world of Blazor and attempting to migrate a project from .NET Core MVC to Blazor WebAssembly. I have successfully configured basic HTML and CSS design, with everything functioning as expected. However, I have encountered an issue on a ...

Ways to verify if fields within an Embedded Document are either null or contain data in Node.js and Mongodb

I need to verify whether the elements in the Embedded Document are empty or not. For instance: if (files.originalFilename === 'photo1.png') { user.update( { userName: userName ...

Using Select2 JS to populate dropdown options with an AJAX response

I have a large list of pincodes ranging from 20,000 to 25,000. I want the user to search for the first 3 digits of a pincode and then trigger an ajax request to fetch a list of pincodes that match those 3 digits. Although I am successfully receiving a res ...

Transform the appearance of buttons within AppBar using Material UI React

Embarking on a new project using React and Node JS has led me into the battle with Material UI. My current challenge is customizing the style of AppBar items, particularly the Buttons. Here's what I have in my "Menu" component: const Menu = () => ...

What is the best way to toggle the visibility of fields on a modal in a Ruby on Rails

I am working on an application that utilizes Rails 4.1 and Ruby 2.1.2. The show.html.erb view for the Assignment Model in this application is as follows: <h1><%= "#{'Source URL'}"%> <div class="source-url"><%= @assignment.so ...

Trouble arises when attempting to create a two-column layout using Material UI's <Grid> component

My goal is to create a two-column layout where each column takes up half of the screen and has equal height. To better illustrate, take a look at this image. The code I've tried so far is not working as expected: import React from "react"; import { ...

Unable to make a POST request to the GitHub v3 API

I'm attempting to generate a public gist using JavaScript without any authentication - all purely client-side. var gist = { "description": "test", "public": true, "files": { "test.txt": { "content": "contents" ...