Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6587227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:53:28+00:00 2026-05-25T16:53:28+00:00

in the following table: <table> <thead> <tr> <th>Th1</th> <th colspan=’2′>Th23</th> <th>Th4</th> </tr> </thead> <tbody>

  • 0

in the following table:

<table>
    <thead>
        <tr>
            <th>Th1</th>
            <th colspan='2'>Th23</th>
            <th>Th4</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Td1</td>
            <td>Td2</td>
            <td>Td3</td>
            <td>Td4</td>
       </tr>
    </tbody>
</table>

For the table cell containing text “Th23”, I’d like to know which cells reside beneath it. In this case, the answer would be the cells containing text “Td2”, and “Td3” respectively.

Are there any DOM properties or built-ins that help with this type of calculation?


@Matt McDonald has a more general solution.

This is what I ended up with:

// get tbody cell(s) under thead cell (first arg)
// if rowIndex===undefined, get from all rows; otherwise, only that row index
// NOTE: does NOT work if any cell.rowSpan != 1
var columnCells = function( th, rowIndex ) {
    // get absolute column for th
    for( var absCol=0, i=0; true; i++ ) {
            if( th.parentNode.cells[i] == th ) break;
            absCol += th.parentNode.cells[i].colSpan;
    }
    // look in tBody for cells; all rows or rowIndex
    var tBody = th.parentNode.parentNode.nextSibling;
    var cells = [];
    for( var r=((rowIndex==undefined)?0:rowIndex); true; r++ ) {
            if( rowIndex!==undefined && r>rowIndex ) break;
            if( rowIndex==undefined && r>=tBody.rows.length ) break;
            for( var c=0; true; c+=tBody.rows[r].cells[c].colSpan ) {
                    if( c < absCol ) continue;
                    if( c >= absCol+th.colSpan ) break;
                    cells.push(tBody.rows[r].cells[c]);
            }
    }
    return cells;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:53:28+00:00Added an answer on May 25, 2026 at 4:53 pm

    Right off the bat, you need to do three things:

    1. Give the table an id attribute for easy selection.
    2. Give the target cell an id attribute for easy selection as well.
    3. Select the cell’s parentNode (row)

    These three things will enable easier table-related calculations.

    Next up is a function that grabs pseudo-properties of the specified cell. In this case, we’re looking for its “start index” (in terms of columns), its “end index” (in terms of columns), and its “width” (end – start, in columns as well).

    From there, you can traverse through the table’s rows and check which cells fall between the start and the end indexes.

    HTML:

    <table id="foo">
        <colgroup span="1">
        <colgroup span="2">
        <colgroup span="1">
        <thead>
            <tr>
                <th>foo</th>
                <th id="example" colspan="2">bar</th>
                <th>baz</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>bing</td>
                <td>bang</td>
                <td>boom</td>
                <td>bong</td>
            </tr>
        </tbody>
    </table>
    

    JS (bear with me):

    function getCellSpanProps(table, row, cell)
    {
        var isRow = (function()
        {
            var i = 0, currentRow;
            for(i;i<table.rows.length;i++)
            {
                currentRow = table.rows[i];
                if(currentRow === row)
                {
                    return true;
                }
                currentRow = null;
            }
            return false;
        }()), 
        cellHasCorrectParent, i = 0, 
        currentCell, colspanCount = 0,
        props;
        if(isRow)
        {
            cellHasCorrectParent = (function()
            {
                return cell.parentNode === row;
            }());
            if(cellHasCorrectParent)
            {
                for(i;i<row.cells.length;i++)
                {
                    currentCell = row.cells[i];
                    if(currentCell === cell)
                    {
                        props = {"start": colspanCount, 
                        "end": colspanCount + cell.colSpan, 
                        "width": (colspanCount + cell.colSpan) - colspanCount};
                        break;
                    }
                    colspanCount += currentCell.colSpan;
                    currentCell = null;
                }
                row = null;
            }
            return props;
        }
    }
    
    function findCellsUnderColumn(table, props)
    {
        var i = 0, j = 0, row, cell,
        colspanCount = 0, matches = [],
        blacklist = {"": true, "NaN": true, "null": true, "undefined": true, 
        "false": true};
        if(blacklist[props.start] || blacklist[props.end] || blacklist[props.width])
        {
            return false;
        }
        for(i;i<table.rows.length;i++)
        {
            row = table.rows[i];
            colspanCount = 0;
            for(j=0;j<row.cells.length;j++)
            {
                cell = row.cells[j];
                if(colspanCount >= props.start && colspanCount < props.end)
                {
                    matches.push(cell);
                }
                colspanCount += cell.colSpan;
                cell = null;
            }
            row = null;
        }
        return matches;
    }
    
    var table = document.getElementById("foo"), 
    example = document.getElementById("example"),
    targetRow = example.parentNode,
    props = getCellSpanProps(table, targetRow, example),
    matches = findCellsUnderColumn(table, props);
    console.log(matches);
    

    Demo: http://jsbin.com/ohohew/edit#javascript,html

    This will determine which cells reside inside the particular column you’re looking for (including the example). You can customize the function to fit your needs if that’s not exactly what you’re looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following mark-up: <table> <thead> ... </thead> <tbody> <tr> <td> <span class='select_me'
I have a DOM structure like the following: <table class=playlist> <thead> <tr> <th>TH1</th> <th
I have the following table: <table class=grid> <thead> <tr> <th>Name</th> <th>Status</th> <tr> </thead> <tbody>
i 've the following html: <table id=mytable> <thead> </thead> <tbody> <tr> <td><a href=# onclick=doSometThingCrazy();
I have the following table: <table> <thead> <tr> <th></th> <th>Female</th> <th>Male</th> </tr> </thead> <tbody>
I have the following code (also in a jsfiddle ): <table> <thead><tr><th>Equation</th><th>Equation</th></tr></thead> <tbody data-bind=template:
I've got following code: $('table.tableElements thead|tbody tr').children().hover(function(){ // How can I do this ↑
I have the following HTML code : <table class=report width=100%> <thead> </thead> <tbody> <tr
I have the following table <thead> <tr> <th>Account ID</th> <th>Code</th> <th>Date Created</th> <th>Date Expires</th>
How to make the following table into a JSON string in jquery/javascript? <table> <thead>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.