Solved

Get the ID of each tr in a table?

Posted on 2007-12-06
9
2,505 Views
Last Modified: 2013-11-19
OK so what i have is an xhtml page, on this page is a table with several rows, each <tr> has in id.  So what i want to do is write a peice of javascript which will get the id of each <tr> in the table and then run this function to hide the specific id rows:

    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }


e.g.

for each tr in the table id="tabel1"
      get the id
      run the  function toggle_visibility function with the current id

thought i im confused as to how to do this?

Gary
0
Comment
Question by:Rocker4500
9 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 20421099
You can retrieve all the tr elements first, and then get their ids:
var trList = document.getElementsByTagName("tr");
if(trList)
 for(var i=0; i<trList.length;++i)
 {
  if(trList.id)
  {
    //alert(trList[i].id);
   toggleVisibility(trList[i].id);
  }
 }
0
 
LVL 82

Expert Comment

by:hielo
ID: 20421108
Typo spotted. Should have been:
 if(trList[i].id)
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20421199
For a specific table:

var td_list = document.getElementById('YOUR_TABLE_NAME').getElementsByTagName('TR');
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:Rocker4500
ID: 20421218
Ok so i now have:

function hide()
{
var trList = document.getElementsByTagName("tr");
if(trList)
 for(var i=0; i<trList.length;++i)
 {
  if(trList[i].id)
  {
    //alert(trList[i].id);
   toggle_visibility(trList[i].id);
  }
 }

}

    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }

which i put in the <body onload="hide();">

But this dosnt seem to work


if i was using badotz suggestion would it now be:

function hide()
{
var tr_list = document.getElementById('YOUR_TABLE_NAME').getElementsByTagName('TR');
if(tr_list)
 for(var i=0; i<tr_list.length;++i)
 {
  if(tr_list[i].id)
  {
    //alert(tr_list[i].id);
   toggle_visibility(tr_list[i].id);
  }
 }

}

    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
0
 
LVL 82

Expert Comment

by:hielo
ID: 20421257
Try using this:
function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display != 'none')
          e.style.display = 'none';
       else
          e.style.display = '';
    }
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20421294
Change 'YOUR_TABLE_NAME' to the ID of your table, obviously...

Also, I would use this:

 for(var i=0; i<tr_list.length;i++)

instead of this:

 for(var i=0; i<tr_list.length;++i)
0
 
LVL 82

Accepted Solution

by:
hielo earned 30 total points
ID: 20421581
The following example does what you want. NOTE: the second row is initially hidden.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
<!--
.hide{display:none;}

-->
</style>

</head>
<body>
<script type="text/javascript">
function toggle_visibility(id) {

       var e = document.getElementById(id);
       if(e.className == 'hide')
          e.className = '';
       else
          e.className = 'hide';
    }
   
function toggleTable(tableName)
{
      var trList = document.getElementById(tableName).getElementsByTagName("tr");
      if(trList)
       for(var i=0; i<trList.length;++i)
       {
        if(trList[i].id)
        {
               toggle_visibility(trList[i].id);
         }
 }

}

</script>
<table id="myData">
      <tr id="a"><td>A</td></tr>
      <tr id="b" class="hide"><td>B</td></tr>
      <tr id="c"><td>C</td></tr>
</table>
<input type="button" onclick="toggleTable('myData')" value="Toggle"/>
</body>
</html>
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20953164
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question