Solved

Get the ID of each tr in a table?

Posted on 2007-12-06
9
2,506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
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).

730 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