?
Solved

Get the ID of each tr in a table?

Posted on 2007-12-06
9
Medium Priority
?
2,508 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 120 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
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).
Suggested Courses

770 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