Solved

Get the ID of each tr in a table?

Posted on 2007-12-06
9
2,503 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to count occurrences of each item in an array.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now