![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: May 2004
Location: Darwin
Posts: 122
|
Hopefully someone knows - I can't seem to find anything, but will keep searching.
So basically I have a block of code, contained within an element: Code:
<table>...</table> <table>...</table> <table>...</table> <hr /> <table>...</table> <table>...</table> <table>...</table> <hr /> ..etc I have been calling element.GetElementsByTagName("table"); but this returns all tables, not just the super tables as above (which are the only ones I want). So is there a way, to grab only those tables? Any help is much appreciate, thanks
__________________
I have: Antec Sonata Plus Case | q6600 @ 2.4Ghz | 2gb Geil DDR 800Mhz | 8800 GTS 512mb | 2TB Raid0 configuration | 24" Dell |
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 |
|
Member
Join Date: Aug 2001
Location: melb
Posts: 5,116
|
i reckon you can test the tables parent tag, if its body, than its a super table ... otherwise its an inner table.
|
|
|
|
|
|
#3 |
|
Member
Join Date: Jul 2005
Location: Melb
Posts: 829
|
if you can use something like jQuery it will make life a lot easier.
jQuery ( and many other Javascript helper libraries ) will let you query and return nodes using a syntax based on CSS. eg in jQuery you would get just the top level child tables with: Code:
element.children('table')
Code:
$('#elementID > table')
Without a JS library you would need to traverse the child nodes manually. Code:
var topLevelTables = new Array();
// loop through all immediate descendants
for ( var i in element.childNodes )
{
// is it a table ?
if( element.childNodes[i].nodeName == 'TABLE' )
{ // then add it to the list
topLevelTables.push( element.childNodes[i] );
}
}
|
|
|
|
|
|
#4 |
|
Member
Join Date: May 2004
Location: Darwin
Posts: 122
|
Thats a good idea - thanks for tips guys
![]() Well, I am not using jQuery in this case, but i'll keep it in mind for future Dogo ![]() Just out of curiosity, in your for loop, the variable i isn't given some int value, is it? As, I would of thought this: Code:
if( element.childNodes[i].nodeName == 'TABLE' ) Code:
if( i.nodeName == 'TABLE' )
__________________
I have: Antec Sonata Plus Case | q6600 @ 2.4Ghz | 2gb Geil DDR 800Mhz | 8800 GTS 512mb | 2TB Raid0 configuration | 24" Dell Last edited by Tr3nt; 12th October 2009 at 11:07 AM. |
|
|
|
|
|
#5 |
|
Member
Join Date: Mar 2005
Posts: 2,870
|
http://docs.jquery.com/Traversing/closest
and http://docs.jquery.com/Traversing/parent might be of help |
|
|
|
|
|
#6 |
|
Member
Join Date: Jul 2005
Location: Melb
Posts: 829
|
actually I realise now that a " for .. in " statement isn't appropriate here.
It can iterate through childNodes as an object rather than an array, maybe it is depending on your browser ? This will get you the index numbers but ALSO the array's member variables ( such as .length ) ie this: Code:
for ( i in element.childNodes )
{
alert(i);
}
( presuming you have 6 sub-tables and I'm testing in FF 3.5 ) if it were a "normal" array, you would get just the index numbers, not the object reference as you had suggested. JS is odd in that way. Anyway, you should rather use : Code:
for ( var i = 0; i < element.childNodes.length; i++ )
Last edited by Dogo; 12th October 2009 at 5:14 PM. |
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|