Click here to Skip to main content
15,887,946 members
Articles / Web Development / HTML
Tip/Trick

Jump Through Sections in HTML Tables

Rate me:
Please Sign up or sign in to vote.
4.71/5 (3 votes)
25 Sep 2013CPOL1 min read 9.9K   6   1
Jumping through TD or TR sections in HTML tables

Introduction

We have some HTML tables that are many pages long divided into a dozen or more sections. We (Ok, I!) needed a way to skip sections without having to scroll or page down. CodeProject allows you to jump from one message thread to the next by using Ctrl + Left/Right/Up/Down Arrow keys. Looking at CP's code (imitation is the sincerest form of laziness), it seemed much more complicated than what I needed, so I decided to write my own.

Using the Code

The code can be summarized as follows:

  1. Count the total number of sections in onload.
  2. Capture when Ctrl key is pressed in onkeydown.
  3. Scroll to appropriate section when arrows are pressed with Ctrl key in onkeydown, while staying within the outer limits of the section count.
  4. Release Ctrl key when no longer needed in onkeyup.

To use the code, add the following JavaScript block inside the HTML's HEAD element.

JavaScript
<script type="text/javascript" language="javascript">
    var index = -1;
    var ctrlSelected = false;
    var sectionCount = 0;
    function getSectionsCount()
    {
        sectionCount = document.getElementsByName("section").length;
    };
    function moveToNextSection()
    {
        var curr = document.getElementsByName("section")[index];
        curr.scrollIntoView(true);
    };
    document.onkeydown = function()
    {
        var e;
        if (window.event)        // IE8 and earlier.
            e = event.keyCode;
        else if (event.which)    // IE9/Firefox/Chrome/Opera/Safari.
            e = event.which;
        if (e == 17)             // Ctrl key.
        {
            ctrlSelected = true;
        }
        else if (e == 35)        // End.
        {
            index = sectionCount;
        }
        else if (e == 36)        // Home.
        {
            index = -1;
        }
        if (ctrlSelected)
        {
            if (e == 38)         // Up arrow.
            {
                if (index > 0)
                {
                    index = index - 1;
                    moveToNextSection();
                }
                return false;
            }
            else if (e == 40)    // Down arrow.
            {
                if (index < sectionCount - 1)
                {
                    index = index + 1;
                    moveToNextSection();
                }
                return false;
            }
        }
        return true;
    };
    document.onkeyup = function()
    {
        var e;
        if (window.event)        // IE8 and earlier.
            e = event.keyCode;
        else if (event.which)    // IE9/Firefox/Chrome/Opera/Safari.
            e = event.which;
        if (e == 17)             // Ctrl key.
        {
            ctrlSelected = false;
        }
    };
</script>

For each TD or TR section, I use id="section" as my section ID.

In the BODY element, add the attribute onload="getSectionsCount()" to count the number of sections once the HTML page is loaded.

Limitations

If you page or scroll away from the section you're at and use this shortcut to skip sections, it continues from where you were last at. I did look into extending it so that it moves up or down from what is actually displayed on the screen, but that would have bloated the code considerably since each browser has its own set of functions and issues.

History

  • Version 1.0 - July 23, 2013 - Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
United States United States
Bassam Abdul-Baki has a Bachelor of Science (BS) degree and a Master of Science (MS) degree in Mathematics and another MS in Technology Management. He's an analyst by trade. He started out in Quality Assurance (QA) and analysis, then dabbled in Visual C++ and Visual C# programming for a while, and then came back to QA and analysis again. He's not sure where he'll be five years from now, but is looking into data analytics.

Bassam is into mathematics, technology, astronomy, archaeology, and genealogy.

Comments and Discussions

 
GeneralMy vote of 3 Pin
satish koladiya26-Aug-13 23:42
professionalsatish koladiya26-Aug-13 23:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.