Jump to content

[SOLVED] traversing table by DOM


janroald

Recommended Posts

html :
  <table id="data" border="1">
  <tr><td></td></tr>
  <tr><td><table><tr><td></td></tr></table></td></tr>
  <tr><td></td></tr>
  </table>

js :
  mytable     = document.getElementById('data');
  mytablebody = mytable.getElementsByTagName('tbody')[0];
  myrows      = mytablebody.getElementsByTagName('tr');
  alert(myrows.length); // 

 

This way myrows contains all tr's including the row in the inner table.

Is there a good way of getting only rows from the inner table?

 

Link to comment
Share on other sites

Sorry, I wrote wrong. It is of course the outer table rows im interested in, not the inner.

The table layout above is just a constructed example.

The actual table may have one or more nested tables inside. I need to traverse only the outer table rows.

Link to comment
Share on other sites

That solution won't do it for me, but thanks for your try, fenway.

The whole point here is to go through the entire table without referring to anything but the table id.

 

I'll look for a good way to get the rows without recursively adding inner table rows, and post it here if i find it.

Link to comment
Share on other sites

Yes, by building the myrows array through a loop, checking parentthis&childthat, I probably would get the result i need. :-)

 

But as a stubborn perfectionist I'm required to dig deeper through the manuals to find the more elegant way. Lucky me... lol

Link to comment
Share on other sites

html :
  <table id="data" border="1">
  <tr><td></td></tr>
  <tr><td><table><tr><td></td></tr></table></td></tr>
  <tr><td></td></tr>
  </table>

js :
  mytable     = document.getElementById('data');
  mytablebody = mytable.getElementsByTagName('tbody')[0];
  myrows      = mytablebody.getElementsByTagName('tr');
  alert(myrows.length); // 

 

This way myrows contains all tr's including the row in the inner table.

Is there a good way of getting only rows from the inner table?

 

You meant only the "outer" rows: every table has a rows attribute that returns a collection of that table's rows, so:

 

  var theRows = document.getElementById('data').rows;

 

Tough, eh?

 

Try the W3C site and read the W3C DOM 2 HTML spec, or use the Mozilla DOM reference:

 

W3C DOM 2 HTML Specification:

<URL: http://www.w3.org/TR/DOM-Level-2-HTML/ >

 

Mozilla DOM reference:

<URL: http://developer.mozilla.org/en/docs/Gecko_DOM_Reference >

 

--

Fred

Javascript FAQ: <URL: http://www.jibbering.com/faq/ >

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.