Solved

Get the ID of each tr in a table?

Posted on 2007-12-06
9
2,502 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn how to count occurrences of each item in an array.

706 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

18 Experts available now in Live!

Get 1:1 Help Now