Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
A table called "tb" has n rows and m columns,
means each row has m "td" mark.

How can I get it's row's first 'td' and column's header when I click one td whick id call 'stdid'?
Posted
Updated 8-Apr-13 6:44am
v4
Comments
Kenneth Haugland 8-Apr-13 12:08pm    
Could you specify what languange you are coding in, and possibly also your own attemts?
zqliu 8-Apr-13 12:47pm    
c# in a web project

As you mention HTML elements, not ASP.NET controls (like GridView), you should get an answer of JavaScript only.

In this case, I'm not sure you ever need to know the particular <td> element, as you can add a handler to the onclick event on each <td> element individually, this way:
http://www.w3schools.com/jsref/event_onclick.asp[^].

Same thing about <th> or any other element.

—SA
 
Share this answer
 
Going with Sergey's assumption that we're talking about front-end code, I will continue down that path and give a JQuery solution, as it sounds like your roadblock is how to traverse the DOM to find the corresponding first column TD.

To get the contents of the first TD of the row containing the TD clicked, you could use
JavaScript
var firstItem = $(this).closest('tr').children(':first-child').html();

The code could be simplified if the first TD isn't itself clickable:
JavaScript
var firstItem = $(this).siblings(':first-child').html();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900