...

View Full Version : getting a particular TD content of table



maria_megha
07-15-2012, 03:24 PM
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

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

jmrker
07-15-2012, 04:56 PM
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...


onclick="alert(this.id)"

or use "alert(this.value)" if you don't really mean to "displayID"

low tech
07-16-2012, 02:45 AM
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


<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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum