Overclockers Australia Forums

OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 11th October 2009, 8:24 PM   #1
Tr3nt Thread Starter
Member
 
Tr3nt's Avatar
 
Join Date: May 2004
Location: Darwin
Posts: 122
Default Javascript - child nodes

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
And within each of those elements is a shit load more tables 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
Tr3nt is offline   Reply With Quote

Join OCAU to remove this ad!
Old 11th October 2009, 8:45 PM   #2
mordy
Member
 
mordy's Avatar
 
Join Date: Aug 2001
Location: melb
Posts: 5,116
Default

i reckon you can test the tables parent tag, if its body, than its a super table ... otherwise its an inner table.
mordy is offline   Reply With Quote
Old 12th October 2009, 10:27 AM   #3
Dogo
Member
 
Dogo's Avatar
 
Join Date: Jul 2005
Location: Melb
Posts: 829
Default

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')
or you can do the same by referencing the top level element by ID (or any other css-compatible method) with:

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] );
    }
}
Dogo is offline   Reply With Quote
Old 12th October 2009, 11:00 AM   #4
Tr3nt Thread Starter
Member
 
Tr3nt's Avatar
 
Join Date: May 2004
Location: Darwin
Posts: 122
Default

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' )
should be:

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.
Tr3nt is offline   Reply With Quote
Old 12th October 2009, 4:34 PM   #5
nathanblogs
Member
 
Join Date: Mar 2005
Posts: 2,870
Default

http://docs.jquery.com/Traversing/closest
and
http://docs.jquery.com/Traversing/parent

might be of help
nathanblogs is online now   Reply With Quote
Old 12th October 2009, 5:03 PM   #6
Dogo
Member
 
Dogo's Avatar
 
Join Date: Jul 2005
Location: Melb
Posts: 829
Default

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);
}
will give 8 msgboxes: "0" , "1" , "2" , "3" , "4" , "5" , "length" , "item"

( 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++ )
Well I learnt something today

Last edited by Dogo; 12th October 2009 at 5:14 PM.
Dogo is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 6:08 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!