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

  • SEARCH
  • Home
  • 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 8893575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:16:56+00:00 2026-06-14T23:16:56+00:00

I was using the answer provided in: How can I find each table cell's

  • 0

I was using the answer provided in:

How can I find each table cell's "visual location" using jQuery?

But it doesn’t seem to work in this case: http://jsfiddle.net/TRr6C/9/

Notice the 1,3 1,4 and 2,4 should be 1,4 1,5 and 2,5

Anyone see whats wrong?

Or is there any better solution to get the cellIndex and rowIndex from a table cell taking into consideration colspan and rowspan?

Here is the code:

function getCellLocation(cell) {

    var cols = cell.closest("tr").children("td").index(cell);
    var rows = cell.closest("tbody").children("tr").index(cell.closest("tr"));
    var coltemp = cols;
    var rowtemp = rows;

    cell.prevAll("td").each(function() {
        cols += ($(this).attr("colspan")) ? parseInt($(this).attr("colspan")) - 1 : 0;
    });

    cell.parent("tr").prevAll("tr").each(function() {
        //get row index for search cells
        var rowindex = cell.closest("tbody").children("tr").index($(this));
        // assign the row to a variable for later use
        var row = $(this);
        row.children("td").each(function() {
            // fetch all cells of this row
            var colindex = row.children("td").index($(this));
            //check if this cell comes before our cell
            if (cell.offset().left > $(this).offset().left) {
                // check if it has both rowspan and colspan, because the single ones are handled before
                var colspn = parseInt($(this).attr("colspan"));
                var rowspn = parseInt($(this).attr("rowspan"));
                if (colspn && rowspn) {
                    if(rowindex + rowspn > rows)
                    cols += colspn;                    
                }
            }

        });
    });

    return {
        rows: rows,
        cols: cols
    };
}
  • 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-06-14T23:16:57+00:00Added an answer on June 14, 2026 at 11:16 pm

    My solution is in form of jQuery plugin since I can imagine more than one purpose of having this information. It can be used as in:

    $("table tbody td").each(function(){ 
        $(this).text( $(this).cellPos().top +","+ $(this).cellPos().left );
    });
    

    The position is in form of { top: rows, left: cols }.
    On its first call, table is scanned and TD elements get data items with their cached position. (Cache can be rebuilt by calling with argument true). All further calls just return that cached value.

    Hope this helps!

    jsfiddle

    Full source:

    /*  cellPos jQuery plugin
        ---------------------
        Get visual position of cell in HTML table (or its block like thead).
        Return value is object with "top" and "left" properties set to row and column index of top-left cell corner.
        Example of use:
            $("#myTable tbody td").each(function(){ 
                $(this).text( $(this).cellPos().top +", "+ $(this).cellPos().left );
            });
    */
    (function($){
        /* scan individual table and set "cellPos" data in the form { left: x-coord, top: y-coord } */
        function scanTable( $table ) {
            var m = [];
            $table.children( "tr" ).each( function( y, row ) {
                $( row ).children( "td, th" ).each( function( x, cell ) {
                    var $cell = $( cell ),
                        cspan = $cell.attr( "colspan" ) | 0,
                        rspan = $cell.attr( "rowspan" ) | 0,
                        tx, ty;
                    cspan = cspan ? cspan : 1;
                    rspan = rspan ? rspan : 1;
                    for( ; m[y] && m[y][x]; ++x );  //skip already occupied cells in current row
                    for( tx = x; tx < x + cspan; ++tx ) {  //mark matrix elements occupied by current cell with true
                        for( ty = y; ty < y + rspan; ++ty ) {
                            if( !m[ty] ) {  //fill missing rows
                                m[ty] = [];
                            }
                            m[ty][tx] = true;
                        }
                    }
                    var pos = { top: y, left: x };
                    $cell.data( "cellPos", pos );
                } );
            } );
        };
    
        /* plugin */
        $.fn.cellPos = function( rescan ) {
            var $cell = this.first(),
                pos = $cell.data( "cellPos" );
            if( !pos || rescan ) {
                var $table = $cell.closest( "table, thead, tbody, tfoot" );
                scanTable( $table );
            }
            pos = $cell.data( "cellPos" );
            return pos;
        }
    })(jQuery);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't find the answer using Google. Works! i = 15 appended = Dark
I can't seem to find an answer to this problem, and I'm wondering if
I have what feels like a simple problem, but I can't seem to find
I'm current using the solution provided in this answer jQuery scroll to element however,
I've searched high and low, but can not find the answer to what I
I know that this has already been asked here but the answer (using a
Right, simple question here. I couldn't find an answer using google and looking here.
I researched for this quite a long time, but could not find proper answer.
I've read every question and answer on this topic I can find on this
I am reading the doc Spring 3.1 security documentation extensively, but I can't find

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.