469,313 Members | 2,559 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,313 developers. It's quick & easy.

Counting links in table rows

Hi

I need to write a script that will allow me to check the value of any link in a table cell. The desired result would be something like this:
Expand|Select|Wrap|Line Numbers
  1. row[0].cell[2].innerHTML
, allowing me to call the contents of the fourth cell in the first row.

The following script may be of use as a starting point:

Expand|Select|Wrap|Line Numbers
  1. <table id="table">
  2. <tr>
  3. <td><a href="#">hello1</a></td>
  4. <td><a href="#">hello2</a></td>
  5. </tr>
  6. <tr>
  7. <td><a href="#">hello3</a></td>
  8. <td><a href="#">hello4</a></td>
  9. </tr>
  10. </table>
  11.  
  12. <script type="text/javascript">
  13. window.onload = function() {
  14.     var table = document.getElementById('table');
  15.     var rows = table.getElementsByTagName('tr');
  16.     for (i=0; i < rows.length; i++) {
  17.         var cells = rows[0].getElementsByTagName('td');
  18.         for (j=0; j < cells.length; j++) {
  19.             alert(cells[1].getElementsByTagName('a').innerHTML); // Desired result is hello2 but returns undefined
  20.         }
  21.     }
  22.  
  23. }
  24. </script>
Thanks
akadeco
Oct 3 '09 #1
2 1393
acoder
16,027 Expert Mod 8TB
Try:
Expand|Select|Wrap|Line Numbers
  1. alert(cells[1].getElementsByTagName('a')[0].innerHTML);
getElementsByTagName() returns a collection.
Oct 6 '09 #2
Expand|Select|Wrap|Line Numbers
  1. window.onload = function() {
  2.     var rows = document.getElementsByTagName('tr');
  3.     for (var i=0; i < rows.length; i++) {
  4.         var cells = rows[i].getElementsByTagName('td');
  5.         alert(cells[0].innerHTML);
  6.     }
  7. }
Is what I was after

Thanks
Oct 8 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Matt Schroeder | last post: by
3 posts views Thread by Steven | last post: by
1 post views Thread by Lord Merlin | last post: by
16 posts views Thread by walexand | last post: by
7 posts views Thread by Patrick Olurotimi Ige | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.