Forum Moderators: open

Message Too Old, No Replies

Accessing HTML Table Cell Data

         

Jon_King

1:19 pm on Sep 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to create a image link that will get data from the current table row like this:

<TR>
<TD >Person Name</TD>
<TD >Person Company</TD>
<TD >Person Title</TD>
<TD>Street Address</TD>
<TD ><div align="center"><a href="http://maps.yahoo.com/maps_result?addr=current-table-row.cell[4]&csz=current-table-row.cell[8]" target="_blank"><img src="images/mapbutton.jpg" width="16" height="12" border="0"></a></div></TD>
<TD >City</TD>
<TD >State</TD>
<TD >Zip</TD>
<TD >Phone</TD>
</TR>

Ideally above would create a button that would open a new window which would get a Yahoo map using the persons address and zip... if I could get the?addr=current-table-row.cell[4]&csz=current-table-row.cell[8] correct in the url. I've tried several HTML DOM methods without success. Perhaps someone can help?

bobming

2:14 pm on Sep 9, 2006 (gmt 0)

10+ Year Member



Just had a play around with the idea and came up with this:


<html>
<head>
<script language ="JavaScript">
function fillTableLinks()
{
var address;
var zip;
var i;
for (i=1; i<document.getElementById('myTable').rows.length; i++)
{
address = document.getElementById('myTable').rows[i].cells[3].childNodes[0].data;
zip = document.getElementById('myTable').rows[i].cells[6].childNodes[0].data;
var map_cell = document.createElement('td');
var link = document.createElement('a');
link.setAttribute('href', 'http://maps.yahoo.com/maps_result?addr=' + address + '&csz=' + zip);
link.setAttribute('target', '_blank');
var image = document.createElement('img');
image.setAttribute('src', 'images/mapbutton.jpg');
link.appendChild(image);
map_cell.appendChild(link);
document.getElementById('myTable').rows[i].appendChild(map_cell);
}
}
</script>
<body onload=fillTableLinks()>
<table>
<tbody id = "myTable">
<TR>
<TD >Person Name</TD>
<TD >Person Company</TD>
<TD >Person Title</TD>
<TD>Street Address</TD>
<TD >City</TD>
<TD >State</TD>
<TD >Zip</TD>
<TD >Phone</TD>
<td>Map</td>
</TR>
<tr>
<td>Joe Bloggs</td>
<td>Bloggs Inc.</td>
<td>Mr</td>
<td>9301 S Santa Monica Blvd</td>
<td>Beverly Hills</td>
<td>CA</td>
<td>90210</td>
<td>12345678</td>
</tr>
</tbody>
</table>
</body>
</html>

It dynamically creates the cell with the link and image inside, and attaches it to the end of the relevant row. Of course, if you really need it to be in a certain position, you can add an empty <td> in each row of the html table and dynamically create the link and image elements to append to that <td>.

Hope this is what you needed!

Jon_King

7:45 pm on Sep 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wow! Now that is a lesson in grabbing HTML table data and working URLs. I really appreciate that and will certainly use it many times, it's such a cool piece of code. Thanks much.

Jon