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 8049089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:41:09+00:00 2026-06-05T06:41:09+00:00

I have a HTML table that contains both ROWSPANs and COLSPANs. How can I

  • 0

I have a HTML table that contains both ROWSPANs and COLSPANs.

How can I find each cell’s “visual location” using jQuery?

For example, here’s a visual representation of my table with each cell populated with what the “visual location” algorithm should return:
(Note: I only really care about cells within the <tbody> and the column reference can be an integer, not an alphabetic character, I’ve only done this to easily highlight the problem)

+--+--+--+--+--+
|  |A |B |C |D |
+--+--+--+--+--+
|1 |A1|B1   |D1|
+--+--+--+--+  +
|2 |A2   |C2|  |
+--+     +--+  +
|3 |     |C3|  |
+--+--+--+--+--+
|4 |A4|B4|C4|D4|
+--+--+--+--+--+
|XYZ           |
+--+--+--+--+--+

I’ve tried implementing the first, however the reference for cell C3 is inaccurate as it doesn’t take into account ROWSPANS. The second link may be able to be merged into the solution of the first, but I can’t figure out how.

  • Working with tables in jQuery | My Tech World
  • What’s the most efficient way to retrieve the column of rows affected by a “rowspan”? – Stack Overflow

I’m hoping to use this as a function called getCellLocation(cell) that will return an associative array that returns the location something like this:

function getCellLocation(cell)
{
  var row_number = parseInt($(cell).parent().prevAll().length) + 1;
  var col_number = 1;
  $(cell).prevAll('td').each(function() {
      col_number += $(this).attr('colspan') ? parseInt($(this).attr('colspan')) : 1;
  });

  var location = new Array();
  location['row'] = row_number;
  location['col'] = col_number;
  location['index'] = $('td').index(cell) + 1;
  return location;
}

$('table td').each(function(i){
  var cell = getCellLocation($(this));
  $(this).prepend('<span class="ref">R' + cell['row'] + ':C' + cell['col'] + ':D' + cell['index'] + '</span>');
});

Here’s the HTML of the example table:

<table border="1" cellspacing="0">
  <thead>
    <tr>
      <th></th>
      <th>A</th>
      <th>B</th>
      <th>C</th>
      <th>D</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>A1</td>
      <td colspan="2">B1</td>
      <td rowspan="3">D1</td>
    </tr>
    <tr>
      <th>2</th>
      <td rowspan="2" colspan="2">A2</td>
      <td>C2</td>
    </tr>
    <tr>
      <th>3</th>
      <td>C3</td>
    </tr>
    <tr>
      <th>4</th>
      <td>A4</td>
      <td>B4</td>
      <td>C4</td>
      <td>D4</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td colspan="5">XYZ</td>
    </tr>
  </tfoot>
</table>
<style> span { background-color: #ffc; margin-right: .5em;} </style>
  • 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-05T06:41:11+00:00Added an answer on June 5, 2026 at 6:41 am

    Here’s my solution:

    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;                    
                    }
                    if(rowspn && rowindex + rowspn > rows) cols +=1;
                }
    
            });
        });
    

    I’m checking for cells which have both colspan and rowspan, because the rest are handled by the first five lines of this code. If the cells have both rowspan and colspan, they should be effecting other cells that are not below OR aside this cell, so I need to search for every cell’s previous cells for interaction.

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

Sidebar

Related Questions

I have an HTML table where the first cell of each row contains text
I have an HTML page (PHP, really) with a table cell that contains a
I have the following html. Its a table that can contains many rows id=rows_x.
I have a row in an html-table that contains only images. (this happens to
I have an HTML table that contains 500 e-mail addresses in this file.html .
I have an example of a simple HTML table that contains a number of
I have the following table that contains checkboxes in each row. When checkbox status
I have a fairly simple dynamic html structure of a table that contains links
I have a table of data that contains about 260 rows. Each of these
I have a MySQL table that contains people's names. Using PHP, what's the best

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.