/    Sign up×
Community /Pin to ProfileBookmark

How can I get the parent <table> attributes of a particular <th>?

Hi,

I have a <th> that I am accessing with javascript, but how can I get its parent table attributes, like id, className, etc.

Thanks,
J

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@DetectAug 01.2007 — Try...

var table = thObj.parentNode.parentNode.parentNode;

table.id
Copy linkTweet thisAlerts:
@DetectAug 01.2007 — Since the hierarchy is TABLE, THEAD, TR, TH, that's why there is three parentNodes.
Copy linkTweet thisAlerts:
@decibelauthorAug 01.2007 — Thanks Detect, that helps. Any shortcuts.
Copy linkTweet thisAlerts:
@steveaAug 01.2007 — Just to be safe (you can never be too safe I say), here's a function that will go up until it finds a table:

<i>
</i>function findParentTable(th){
var ele = th.parentNode; while( ele.tagName != "TABLE" ){ ele = ele.parentNode; }
return ele;
}


You could use it like so:
<i>
</i>var table = findParentTable(th);

//Get table id
var table_id = table.getAttribute('id');
//Or
var table_id = table.id;

//Get table class
var table_className = table.getAttribute('class');
//Or
var table_className = table.className;
Copy linkTweet thisAlerts:
@decibelauthorAug 01.2007 — That is nice too, thanks stevea
×

Success!

Help @decibel spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...