By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

Counting links in table rows

P: 7
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
Share this Question
Share on Google+
2 Replies


acoder
Expert Mod 15k+
P: 16,027
Try:
Expand|Select|Wrap|Line Numbers
  1. alert(cells[1].getElementsByTagName('a')[0].innerHTML);
getElementsByTagName() returns a collection.
Oct 6 '09 #2

P: 7
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.