Ways to automatically scroll through an associated HTML table

Managing two related tables that display the same information in different languages can be a tricky task. In my case, when I use the Key down or Key up functions, only one table scrolls while the other remains stationary. How can I synchronize their scrolling movements?

Here is a snippet of the HTML5 code:

<div id="first_table">
    <table id="first_header">
      <tr>
        <th class="field_1">1</th>                
        <th class="field_2">2</th>
        <th class="field_3">3</th>
        <th class="field_4">4</th>
        <th class="field_5">5</th>
      </tr>
    </table>
    <div id="first_table_container">
      <table id="first_text_table">
       <tbody id="first_text_table_body">
       </tbody>
      </table>
    </div>
    <div id="first_current">
     <textarea id="first_current_text" ></textarea>                           
     </div> 
</div>

<div id="second_table">
    <table id="second_header">
      <tr>
        <th class="field_1">1</th>                
        <th class="field_2">2</th>
        <th class="field_3">3</th>
        <th class="field_4">4</th>
        <th class="field_5">5</th>
      </tr>
    </table>
    <div id="second_table_container">
      <table id="second_text_table">
       <tbody id="second_text_table_body">
       </tbody>
      </table>
    </div>
    <div id="second_current">
     <textarea id="second_current_text" ></textarea>                           
     </div> 
</div>

Below are some CSS rules:

#first_table
{
width:90%;
float: left;
border-radius: 5px;
border-color: black;                                
border-collapse: collapse;    
margin-right:33px;
margin-bottom: 1%; 
}       
#second_table
{
width:90%;
float: right;
border-radius: 5px;
border-color: black;                                
border-collapse: collapse;    
margin-right:30px;
margin-bottom: 1%; 
}
#first_table_container, #second_table_container 
{
height: 200px;
overflow-y: scroll;
border-style: solid;
border-width: 0px;
border-color: #5e5e5e;                           
border-bottom-width:1px;
}

Lastly, let's take a look at the JavaScript (jQuery) code:

if (e.keyCode == 38) { // up
                var index = $('#first_text_table_body tr > td').index();
                $('#second_text_table_body').scrollTo(index);
}

I've experimented with using scrollTo(), but unfortunately, I haven't been able to achieve the desired synchronization between the two tables. My objective is for both tables to scroll simultaneously when the key up function is triggered.

Thank you in advance!

Answer №1

JSFiddle

HTML Code Snippet

<div id="first_table">
    <table id="first_header">
        <tr>
            <th class="field_1">1</th>
            <th class="field_2">2</th>
            <th class="field_3">3</th>
            <th class="field_4">4</th>
            <th class="field_5">5</th>
        </tr>
    </table>
    <div id="first_table_container">
        <table id="first_text_table">
            <tbody id="first_text_table_body">
                <!-- Rows with data -->
            </tbody>
        </table>
    </div>
    <div id="first_current">
        <textarea id="first_current_text"></textarea>
    </div>
</div>

<div id="second_table">
    <table id="second_header">
        <tr>
            <th class="field_1">1</th>
            <th class="field_2">2</th>
            <th class="field_3">3</th>
            <th class="field_4">4</th>
            <th class="field_5">5</th>
        </tr>
    </table>
    <div id="second_table_container">
        <table id="second_text_table">
            <tbody id="second_text_table_body">
                <!-- Rows with data -->
            </tbody>
        </table>
    </div>
    <div id="second_current">
        <textarea id="second_current_text"></textarea>
    </div>
</div>

CSS Styling

#first_table {
    width:45%;
    float: left;
    border-radius: 5px;
    border-color: black;
    border-collapse: collapse;
    margin-right:33px;
    margin-bottom: 1%;
}
#second_table {
    width:45%;
    float: right;
    border-radius: 5px;
    border-color: black;
    border-collapse: collapse;
    margin-right:30px;
    margin-bottom: 1%;
}
#first_table_container, #second_table_container {
    height: 200px;
    overflow-y: scroll;
    border-style: solid;
    border-width: 0px;
    border-color: #5e5e5e;
    border-bottom-width:1px;
}

Javascript Functionality

$(document).ready(function () {
    $(document).keydown(function (e) {
        console.log(e);
        if (e.keyCode == 38) { //up
            $("#first_table_container").animate({
                scrollTop: 0
            });
            $("#second_table_container").animate({
                scrollTop: 0
            });
        }
        if (e.keyCode == 40) { //down
            $("#first_table_container").animate({
                scrollTop: 200
            });
            $("#second_table_container").animate({
                scrollTop: 200
            });
        }
    });
});

The scrollTop value is used to determine the position to scroll to when keyboard events occur. This code snippet provides a starting point for implementing scrolling functionality based on key presses.

Remember to tidy up the code and continue customizing it according to your requirements.

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

"Exploring the world of click events in Javascript

Is there a way to use onclick event in my search form utilizing AJAX to direct the user to another page with an ID parameter? At present, selecting an option from the drop-down menu immediately redirects me to a new page (some_page.php? with the correct c ...

Utilizing Javascript for Cucumber and Protractor Automation - Save code in a separate document

I am utilizing Cucumber and Protractor in conjunction with Node.js to perform testing on my web application. I rely on javascript for coding along with chai expectations. My current task involves the necessity of saving certain results into a separate text ...

Is it possible for a synchronous (blocking) ajax request to delay the rendering of a website's UI?

This query pertains to jQuery, though not exclusively focused on it. To sum it up: Can a synchronous ajax call prevent a regular button from being clicked on? My experimentation shows no issues, but compatibility with other browsers could be a concern. ...

The commitment remained unfulfilled following a validation error

I have a login form that uses the promise object for authentication. Everything works smoothly except when the form validation is triggered. Below is my HTML code: <form id="signin" class="signinform" autocomplete="off"> <span ng-show="err ...

In order to avoid conflicts, please update the version code of your APK as there is currently one with version code 1. How can I go about changing the version?

My attempt to upload an apk file on Google Play has hit a roadblock. The apk file was created using Unity3D. Google Play is giving me an error message stating that I need to use a different version code for my APK because a version with code 1 already exis ...

Defining an exact path within an Express Router when utilizing an array of routes

The Documentation for Express clearly states that you can provide an array of routes to the app.use method for a specific middleware. Furthermore, they explain how to separate routers into different files which is detailed here. However, it's not ev ...

Stop browsers from automatically filling out fields that are hidden

Our method involves jQuery to conceal irrelevant fields based on previous responses. It's puzzling how the browser (Chrome) has somehow autofilled the user's email address into one of them even though autocomplete is disabled. The field's ID ...

The request was unsuccessful in being sent

I recently started working with MVC and am currently developing a web app using it. I'm in the process of trying to send a GET request. $(document).ready(function () { var query = $('#productQuery').val(); alert("Getting started."); ...

Jquery and adaptability

I currently have a scrollTo anchor link script on my website that has an offset of -35px to account for my 35px high navigation bar. However, I find that this offset may need to be adjusted for different screen resolutions, such as smartphones or tablets ...

Preserve line breaks within HTML text

Utilizing the powerful combination of Angular 5 and Firebase, I have successfully implemented a feature to store and retrieve movie review information. However, an interesting issue arises when it comes to line breaks in the reviews. While creating and edi ...

Creating a for loop using jQuery to append the value of [i] to the string "id" for the element

As a newcomer to programming, my code may not be the best. I am attempting to automate game results (1 / X / 2) based on the input of home and away goals in a form. To achieve this, I need assistance with my jQuery for loop. Here is the current code: for ...

Creating a see-through effect in Three.js with React Fiber

I'm currently working with react fiber and struggling to make the background of my child scene transparent. Below is my root component containing the main Canvas element: export const Splash = () => { const { intensity, distance, colo ...

Are you looking for a way to prevent the default behavior of an event in angular-ui-router 1.0.x? Check out

Recently, I made a change in my code where I replaced $stateChangeStart with $transitions.onStart $rootScope.$on('$stateChangeStart', function(e, ...){ e.preventDefault(); // other code goes here... }); Now, the updated code looks lik ...

Regular pattern with Kubernetes cluster endpoint utilizing either IP address or fully qualified domain name

In my Angular/typescript project, I am working on building a regex for a cluster endpoint that includes an IP address or hostname (FQDN) in a URL format. For instance: Example 1 - 10.210.163.246/k8s/clusters/c-m-vftt4j5q Example 2 - fg380g9-32-vip3-ocs.s ...

What is the best way to align a button with a title on the right side of a page?

Is it possible to align my button to the top right, at the same height as the title in my design? Here is an example: view image here This is what I have achieved so far: view image here I suspect there may be an issue with my divs. The button doesn&a ...

What is the best way to combine Node.js MySQL tables into a JSON format and transfer the data to the client?

I'm looking for a way to retrieve MySQL related tables as JSON from NodeJS and send it back to the client. For example, let's say I have a 'students' table and a 'studentCountry' table. The 'students' table has a fie ...

Access to the remote resource at https://localhost:8000/users/login has been blocked due to a Cross-Origin Request restriction imposed by the Same Origin Policy

Attempting to send a post message to the /users/signup endpoint results in the same error occurring repeatedly. Below is the code snippet from server.js: const https = require('https'); const fs = require('fs'); var path = require(&apos ...

Revamp the category style in Osclass

Although the Osclass is a useful tool, I have encountered some issues with it. Here is the code snippet: <div class="cell selector"> <?php osc_categories_select('sCategory', null, __('Select country/city', ...

Vertical Spacing in Bootstrap 3: Eliminating Gaps Between Divs

I am looking to eliminate the gap between certain div elements and the div positioned below it. This space is created when one div is taller than another in a column. For instance, take a look at this example: http://www.bootply.com/Hc2aO5o4bG Essential ...

Horizontal Alignment of Various Elements

Is there a better way to align these elements without using margin-top on the check mark icons to center them with the buttons? Avoiding Margin-Top <div class="icon check" style="margin-top:11px;"></div> You can view a live example here: JS ...