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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:44:35+00:00 2026-06-03T13:44:35+00:00

Not using jQuery, what is the most efficient way of getting the Row and

  • 0

Not using jQuery, what is the most efficient way of getting the Row and Col (X, Y) of a onClick even within a table?

I would think that assigning a click listener to just the table, and let it bubble to the top would work well, but really it just gives me the HTMLTableElement. What I want to get from it is the Col number, and Row number from this one listener. Is that possible?

window.onload = function () {
 document.getElementsByTagName('table')[0].addEventListener('click', function() {
  alert(this.tagName);
 }, false);
}
  • 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-03T13:44:36+00:00Added an answer on June 3, 2026 at 1:44 pm

    You could bind to the table, but that would leave open the possibility that you might click within the spacing between cells (which doesn’t have a row or cell index). I have decided in the example below that I would bind to the cells themselves, and thus ensure I would always have a row and cell index.

    var tbl = document.getElementsByTagName("table")[0];
    var cls = tbl.getElementsByTagName("td");
    
    function alertRowCell(e){
      var cell = e.target || window.event.srcElement;
      alert( cell.cellIndex + ' : ' + cell.parentNode.rowIndex );
    }
    
    for ( var i = 0; i < cls.length; i++ ) {
      if ( cls[i].addEventListener ) {
        cls[i].addEventListener("click", alertRowCell, false);
      } else if ( cls[i].attachEvent ) {
        cls[i].attachEvent("onclick", alertRowCell);
      }
    }
    

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

    I suppose you could safely bind to the table itself too, and perform a check against the source element to see if it was a cell or not:

    var tbl = document.getElementsByTagName("table")[0];
    
    function alertRowCell (e) {
      var cell = e.target || window.event.srcElement;
      if ( cell.cellIndex >= 0 )
        alert( cell.cellIndex + ' : ' + cell.parentNode.rowIndex );
    }
    
    if ( tbl.addEventListener ) {
      tbl.addEventListener("click", alertRowCell, false);
    } else if ( tbl.attachEvent ) {
      tbl.attachEvent("onclick", alertRowCell);
    }
    

    Demo: http://jsbin.com/isedel/5/edit

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

Sidebar

Related Questions

I would like to determine the most efficient way (using jquery, or dom) to
[edit] I am NOT using jquery in this app. Looking for a way to
I am using jquery for changing image path but its not working for asp.net
I am using jQuery .load to call an action but it is not found.
Using jQuery, the following is not working in FF, but it is in IE
I have added some animation using jquery but it's not working: $('.test-container').click(function(){ $(this).css('background-color', 'rgb(119,
How can I select elements (using jQuery) that do not contain any elements? For
I have a little problem about using jQuery (I really do not know jQuery
I have a problem with selecting not first selector using attributeContains jQuery('div[id*=abc]:not(:first)').hide();
I am trying to get an element using JQuery's selector, but it does not

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.