INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Document Object Model (DOM)

Document Object Model (DOM) For a Table by mwolf00
Posted: 15 May 03 (Edited 15 May 03)

Consider the table

<table id="myTable">
 <tr>
  <td>cell 1</td>
  <td>cell 2</td>
 </tr>
 <tr>
  <td>cell 3</td>
  <td>cell 4</td>
 </tr>
</table>

<script>
// cell 4 can be referenced in several ways
tableNode = document.getElementById("myTable")

// method 1
// remember that the first child of a tableNode is always
// the TBODY node even though you don't write it
val1 = tableNode.firstChild.firstChild.nextSibling.firstChild.nextSibling.innerHTML
// literally - table.tableBody.firstRow.secondRow.firstCell.secondCell.innerHTML

// method 2
// here you use the rows array followed by the cells array
// remember that JavaScript arrays start at [0], so [1] refers to the second element of the array
val2 = tableNode.firstChild.childNodes[1].childNodes[1].innerHTML

// method 3
// you could also cheat using the lastChild nodes
// since we want the last child row and last child cell

val3 = tableNode.lastChild.lastChild.lastChild.innerHTML

</script>

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close