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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:58:33+00:00 2026-05-25T23:58:33+00:00

I have an onclick function attached to each <td> element. The function needs to

  • 0

I have an onclick function attached to each <td> element. The function needs to know the placement in the table (row and column).

I found an attribute called rowIndex for <tr>s, but that would involve getting the parent element and still doesn’t help with column number.

Here is my function so far (it’s within a loop, so it’s attaching to every td)

td.onclick = function(event) {
  event = (event) ? event : window.event;
  console.log('rowIndex, colIndex');
}

I could figure this out with jQuery, but I’m trying to do without.

  • 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-25T23:58:34+00:00Added an answer on May 25, 2026 at 11:58 pm

    My first thoughts were:

    var rows = document.getElementsByTagName('tr');
    var cells = document.getElementsByTagName('td');
    var cellsPerRow = cells.length/rows.length;
    var i = 0;
    for (r=0;r<rows.length;r++){
        for (c=0;c<cellsPerRow;c++){
            cells[i].setAttribute('data-col',c + 1);
            cells[i].setAttribute('data-row',r + 1);
            cells[i].onclick = function(){
                var row = this.getAttribute('data-row');
                var col = this.getAttribute('data-col');
                alert('Row: ' + row + '; col: ' + col);
            };
        i++;
        }
    }
    

    JS Fiddle demo.

    This was then refined to:

    var cells = document.getElementsByTagName('td');
    
    for (i=0;i<cells.length;i++){
        cells[i].onclick = function(){
            var row = this.parentNode.rowIndex + 1;
            var col = this.cellIndex + 1;
            alert('Row: ' + row + '; col: ' + col);
        };
    }
    

    JS Fiddle demo.

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

Sidebar

Related Questions

I have a little javascript function which is attached to an onClick event of
I have an onclick function that I want to add an anchor to the
I have a JS function - triggered with onclick -, that is dynamically changing
I have an onclick handler for an <a> element (actually, it's a jQuery-created handler,
On Window 's load, every DD element inside Quote_App should have an onCLick event
I have an onclick event attached to a button. Clicking the button adds a
I have a form on ASP .NET page, with button onclick event attached to
I have a LinkButton server control which have some attached method in OnClick Event
I have an event attached to a div element that will remove the element
We have lots of javascript functions, which are usually handled via the onclick function.

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.