Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tables and Rows Handling

Status
Not open for further replies.

hjmc

Technical User
Nov 20, 2002
38
GB
When a user clicks a row in a Table, I am trying to get the index of that row within the table.

When I get the index of the row I want to assign it to to hidden field so that I can process it on the server.

Current attempt is

var theCell, theRow, theTab;

// get the row that was clicked
if (!e) var e = window.event;
if (e.target) theCell = e.target;
else if (e.srcElement) theCell = e.srcElement;
if (theCell.nodeType == 3) // defeat Safari bug
theCell = theCell.parentNode;

theRow = theCell.parentNode;
theTab = theRow.parentNode
var rows = theTab.getElementsByTagName("tr")
var max = rows.length
var row
var index = 0
for (index=0; index<max-1; index++)
{
row = rows[index]
if (row = theRow)
{
alert(index)
return;
}

}

Can any one help on this.

 
May seem like a simple answer but can you just assign an OnClick event to your table rows as they are rendered? Just point to the same function and pass along the rowid.


I have used this with loops to include the loop variable as the row id.

An example:
Code:
for (i=0;i<=workspace[ws_index].item_index;i++)
		{
			if (workspace[ws_index].items[i].department)  // Only print populated array elements
			{
				
				innerHTML += '<tr height="23" id='+i+' class=c2 onclick="sel_row('+i+')" onmouseover="ov('+i+')" onmouseout="ot('+i+')" >';
				innerHTML += '<td align="left">'+workspace[ws_index].items[i].department+'</td>';
				innerHTML += '<td align="center">'+workspace[ws_index].items[i].name.substring(0,24)+'</td>';
				innerHTML += '<td align="center">'+workspace[ws_index].items[i].qty+'</td>';
				innerHTML += '<td align="right">'+fixeddecimal(workspace[ws_index].items[i].copay)+'</td>';
				innerHTML += '<td align="right">'+fixeddecimal(workspace[ws_index].items[i].price)+'</td>';
				innerHTML += '<td align="right">'+workspace[ws_index].items[i].subtotal+'</td>';
				innerHTML += '</tr>';
			}
		 }

Hope this helps.

LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top