www.webdeveloper.com
Results 1 to 6 of 6

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

  1. #1
    Join Date
    Dec 2004
    Posts
    439

    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

  2. #2
    Join Date
    Jul 2007
    Posts
    111
    Try...

    var table = thObj.parentNode.parentNode.parentNode;
    table.id

  3. #3
    Join Date
    Jul 2007
    Posts
    111
    Since the hierarchy is TABLE, THEAD, TR, TH, that's why there is three parentNodes.

  4. #4
    Join Date
    Dec 2004
    Posts
    439
    Thanks Detect, that helps. Any shortcuts.

  5. #5
    Join Date
    Jun 2007
    Posts
    104
    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:

    Code:
    function findParentTable(th){
    	var ele = th.parentNode; while( ele.tagName != "TABLE" ){ ele = ele.parentNode; }
    	return ele;
    }
    You could use it like so:
    Code:
    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;

  6. #6
    Join Date
    Dec 2004
    Posts
    439
    That is nice too, thanks stevea

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles