Is there a way to implement a scrollbar that only scrolls through one specific column in an HTML table?

I need help adding a scrollbar to a specific column in an HTML table. Take a look at this scenario https://jsfiddle.net/6wpdc4tL/:

https://i.stack.imgur.com/svzIg.png

This table should have two scrollbars, one for the light blue SCROLL column and another for the light green SCROLL2 column. Each scrollbar should only affect its respective column and scroll the entire column at once. The Row and Sec Row columns should remain the same width and not be affected by the scroll.

<table>
    <tr>
      <th class="headcol">1</th>
      <td class="long1">SCROLL SCROLL SCROLL SCROLL</td>
      <th class="middlecol">1</th>
      <td class="long2">SCROLL2 SCROLL2 SCROLL2 SCROLL2</td>
    </tr>
    <tr>
      <th class="headcol">2</th>
      <td class="long1">SCROLL SCROLL SCROLL SCROLL</td>
      <th class="middlecol">2</th>
      <td class="long2">SCROLL2 SCROLL2 SCROLL2 SCROLL2</td>
    </tr>
  </table>

Can someone guide me on how to add a scrollbar that specifically scrolls one column in an HTML table?

Answer №1

To achieve the desired outcome, I created a piece of JavaScript code to modify the table structure in such a way that the column width could be set to show only one scrollbar at the bottom.

 const classes = ['headcol', 'long1', 'middlecol', 'long2']
  
 const modifiedTable = document.createElement("table");
 modifiedTable.setAttribute("id", "modifiedTable");
 const row = document.createElement('tr');
 
 classes.forEach((c, idx) => {
   const cell = document.createElement(idx % 2 === 0 ? 'th' : 'td');
   cell.setAttribute('class', `${c}new`)
   const div = document.createElement('div');
   const subTable = document.createElement('table');

   const nodes = document.querySelectorAll(`.${c}`);
   nodes.forEach(n => {
      const newRow = document.createElement("tr");
      newRow.appendChild(n);
      subTable.appendChild(newRow);
   });

   div.appendChild(subTable)
   cell.appendChild(div)
   row.appendChild(cell);
 });
 
 modifiedTable.appendChild(row);
 document.body.appendChild(modifiedTable);
 
.headcolnew, .middlecolnew {
  vertical-align: top;
}

.long1new, .long2new {
  max-width: 125px;
  overflow-x: scroll;
  white-space: nowrap;
}
<table>
    <tr>
      <th class="headcol">1</th>
      <td class="long1">SCROLL SCROLL SCROLL SCROLL</td>
      <th class="middlecol">1</th>
      <td class="long2">SCROLL2 SCROLL2 SCROLL2 SCROLL2</td>
    </tr>
    <tr>
      <th class="headcol">2</th>
      <td class="long1">SCROLL SCROLL SCROLL SCROLL</td>
      <th class="middlecol">2</th>
      <td class="long2">SCROLL2 SCROLL2 SCROLL2 SCROLL2</td>
    </tr>
  </table>

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

Height of Owl Carousel on mobile devices

On my desktop, I have an image that covers the entire screen using Owl Carousel. However, when viewed on a phone device, the same image only takes up one-third of the screen size. How can I adjust the height so that it appears taller on a phone? I' ...

Use CSS and JavaScript to create a visually appealing and interactive tree structure. Give the tree a dynamic design

I have been experimenting with CSS and JavaScript to create a dynamic graph based on a data table. You can view the graph that I need to replicate using CSS and JavaScript in this PDF. The example provided below showcases what I am attempting to achieve, a ...

Is it necessary to reset (local storage, session storage) once the countdown timer reaches zero?

I have successfully implemented a countdown timer using session storage in my code. However, I am looking to enhance its functionality further by: Clearing the local session if the user leaves the site before the countdown timer finishes. Automatically c ...

Issues with Adaptive Headers

* { box-sizing: border-box; font-family: sans-serif; margin: 0; padding: 0; } html, body { font-size: 14px; } header { width: 100vw; height: 50px; background-color: #222; margin: 0 auto; padding: 0; display: flex; justify-content: ...

Guide on comparing an object against an array and retrieving a specific output

If I were to create a data structure like this: const carObj = {"1234":"Corvette","4321":"Subaru","8891":"Volvo"}; And also have an array that contains the IDs: const myArray = [1234, 4321, 8891, ...

Tips for locating a file within an input element in HTML using jQuery

I attempted to upload a file using jQuery's $.ajax call and formData. To add the file to the formData, I initially tried: var fd = new FormData(); fd.append($('#myFileInput)); However, this method failed. Next, I attempted: var fd = new Form ...

What is the best way to ensure an image is responsive in CSS and aligns perfectly in height with its neighboring text?

There is an image within the <img> tag and some text inside the <p> tag, both enclosed in a <div>. Although the image is responsive and resizes proportionally when the browser window changes size, there is an issue where as the text wrap ...

Setting up pagination for displaying data from a database on the client-side: a step-by-step guide

Greetings to all my fellow programmers on Stack Overflow! I am currently in the process of developing an e-commerce application using JSP. The products for the application are stored in a server-side database (Mysql) within a table named product. The prod ...

Using CSS to position elements absolutely while also adjusting the width of the div

In one section of my website, I have a specific div structure. This structure consists of two divs stacked on top of each other. The first div is divided into two parts: one part with a width of 63% and another part with a button. Beneath the first div, t ...

Having issues with implementing CSS3 animations on my webpage

I'm having trouble implementing a bouncing mouse animation on my website. The code works perfectly on another site, but on mine, it's not functioning at all. Here is the CSS code: .mouse { display: block; margin: 0 auto; text-alig ...

Combining two divs to share the same linear gradient and shadow effect

Greetings to all our valued partners! I am seeking guidance on how to achieve the following task for a web application I am developing: Within my webapp, I have designed a stylish NavBar similar to the examples depicted in the images (in AdobeXD it is di ...

Tips for utilizing ion view within an ionic 2 application

In my original use of Ionic 1, I placed the footer after the <ion-content> and before . However, in the new Ionic 2, I can't seem to find any reference to <ion-view>. My specific need is to have two buttons at the bottom of the screen fol ...

Rotating an image as it approaches the footer: a step-by-step guide

I'm trying to add a navigation arrow on my website that will rotate to point to the top when it reaches the footer. When clicked on, it should scroll back up to the top. The arrow already has a scrolling effect, but I need help figuring out how to m ...

The connection between an HTML form and PHP is not being established

variable-passing.php `<form method="post" action="variable-catching.php"> <input type="text" name="name1"/><br/> <input type="text" name="name2"/><br/> <input type="text" name="name3"/><br/> <input type="te ...

Ensure history.back() does not trigger Confirm Form Resubmission

Within my web application, every form submission is directed to an action folder. Once the process is complete, the user is redirected back to their original location. However, a problem arises when the user performs an action that requires the application ...

Placing the input-group-addon above the text-input for better positioning

Feeling overwhelmed trying to finalize a simple form due to CSS issues? Check out this image for a visual of the problem: https://i.stack.imgur.com/PgCmN.jpg The label is slightly off to the left, and the button appears larger than the input field. The i ...

Node.js client-sessions malfunction when not utilized as a primary callback

When utilizing express.Router, I'm attempting to create a middleware for a particular router in my application that will establish and handle a session. However, this approach fails if the client-sessions session is not invoked directly by Router().us ...

Streamlining the process of implementing click events on elements selected by class using jQuery

Slowly but surely, I am gaining familiarity with jQuery and have reached the point where I desire to abstract my code. However, I am encountering issues when attempting to define click events during page load. Within the provided code snippet, my objectiv ...

Tips for creating a seamless horizontal scrolling effect in Angular when hovering (automatically)

One of the components I'm working on features a gallery with an X axis orientation. <div class="gallery"> <div (mouseenter)="scrollTo('left', $event)" (mouseleave)="clearIntervalRepeater()" class="left"></div> < ...

Menu drop down offset in Internet Explorer 7

Having some difficulty with a dropdown menu specifically in IE7. The menu functions correctly in all other browsers, but in IE7 it seems to be misaligned for some reason. Any suggestions or insights would be greatly appreciated. Please refer to the menu co ...