Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-15-2012, 03:24 PM   PM User | #1
maria_megha
New Coder

 
Join Date: Dec 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
maria_megha is an unknown quantity at this point
getting a particular TD content of table

hi all,
I am trying to alert the td content of a table . I pass the company Id as the parameter and need to alert the company name.I am able to alert only the first company.What needs to be done.?Please help
Code:
<html>
<head>
<script type="text/javascript">
function displayID(id)
{
var match=id.trim();
alert(match);
var table = document.getElementById('MyTable');
        for (var r = 0, n = table.rows.length; r < n; r++) {
            for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
                alert(table.rows[r].cells[c].innerHTML);
		if(table.rows[r].cells[c].innerHTML==match){
alert("Found!");
alert(table.rows[r].cells[c-1].innerHTML)
}
            }//for c
        }//for r
}
</script>
</head>
<body>
<table border="1" id="MyTable">
<tr>
<td> </td>
<td>Company Name </td>
<td> Company Id </td>
<td>Status </td>
</tr>
<tr>
<td><input type="radio" name="radio_newcontact" id="radio_newcontact" value="ABC" onclick="displayID(this.value)"/></td>
<td id="ABC tradin Co">ABC Trading Co</td>
<td id="ABC">ABC</td>
<td>Active</td>
</tr>
<tr>
<td><input type="radio" name="radio_newcontact" id="radio_newcontact" value="DEF" onclick="displayID(this.value);"></td>
<td id="DEF Co">DEF Co</td>
<td id="DEF"> DEF</td>
<td>Active</td>
</tr>
<tr>
<td><input type="radio" name="radio_newcontact" id="radio_newcontact" value="GHI" onclick="displayID(this.value);"></td>
<td id="GHI LTD">GHI LTD</td>
<td id="GHI"> GHI</td>
<td>Active</td>
</tr>
</table>
</body>
</html>
maria_megha is offline   Reply With Quote
Old 07-15-2012, 04:56 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

Part of your problem is that you have duplicated ID values.
ID values must be unique. Otherwise getElementById() gets very confused.

Also your onclick function calls a loop that looks at each cell of the table.
Why?

What is it exactly that you want to alert? Just the Company name or something else?
If it is just the ID then this should work...
Code:
onclick="alert(this.id)"
or use "alert(this.value)" if you don't really mean to "displayID"
jmrker is offline   Reply With Quote
Old 07-16-2012, 02:45 AM   PM User | #3
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi

note you had spaces in your table so your match would fail
<td id="DEF"> DEF</td>

Also you cannot have duplicated ID's -- ID's are unique -- no two ID's can have the same name on a page as jmrker has mentioned.


try this and see if it helps

Code:
<html>
<head>
<!-- http://www.codingforums.com/showthread.php?t=267923 -->
<script type="text/javascript">
function displayID(id)
{
  var match=id.trim();
  alert("looking to match  "+match);
  var table = document.getElementById('MyTable');
  for (var r = 0; r < table.rows.length; r++) {
    for (var c = 0; c < table.rows[r].cells.length; c++) {
      //alert(table.rows[r].cells[c].innerHTML);
      if(table.rows[r].cells[c].innerHTML==match){
        alert("Found!");
        alert(table.rows[r].cells[c-1].innerHTML);
      }   
    }//for c
  }//for r
}
</script>
</head>
<body>
<table border="1" id="MyTable">
<tr>
<td></td>
<td>Company Name </td>
<td> Company Id </td>
<td>Status </td>
</tr>
<tr>
<td><input type="radio" name="radio_newcontact" id="radio_newcontact0" value="ABC" onclick="displayID(this.value)"/></td>
<td id="ABC tradin Co">ABC Trading Co</td>
<td id="ABC">ABC</td>
<td>Active</td>
</tr>
<tr>
<td><input type="radio" name="radio_newcontact" id="radio_newcontact1" value="DEF" onclick="displayID(this.value);"></td>
<td id="DEF Co">DEF Co</td>
<td id="DEF">DEF</td>
<td>Active</td>
</tr>
<tr>
<td><input type="radio" name="radio_newcontact" id="radio_newcontact2" value="GHI" onclick="displayID(this.value);"></td>
<td id="GHI LTD">GHI LTD</td>
<td id="GHI">GHI</td>
<td>Active</td>
</tr>
</table>
</body>
</html>
LT
__________________
Ask not what can I do for myself, but what can I do for others

"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown
low tech is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, table

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:12 PM.


Advertisement
Log in to turn off these ads.