skip over table head values

  • Thread starter Matt Williamson
  • Start date
M

Matt Williamson

I have the following function that I found and modified from another post

function setValues(evt, table){
var el;
evt=evt||window.event;
el=evt.target||evt.srcElement;

for(;el&&el!=table;el=el.parentNode)
//if(el.nodeName.toLowerCase()!="th")
// if(el.nodeName.toLowerCase()=="th")
// continue;
if(el.nodeName.toLowerCase()=="tr")
break;

document.ds.server.value = el.cells[0].textContent;
document.ds.drive.value = el.cells[1].textContent;
document.ds.submit();
}

I'm calling it in the onclick event for a Table with id="ds". It works great
by allowing me to click any row in the table and passing the first 2 cells
as hidden input fields, but I can't figure out how to skip the Table heads.
I'm using another script to be able to sort the rows by clicking the table
head row and I don't want this to run when I do that. The commented lines
are the things that I have tried so far and failed.

Any help appreciated.

Matt
 
I

Ivo

for(;el&&el!=table;el=el.parentNode)
//if(el.nodeName.toLowerCase()!="th")
// if(el.nodeName.toLowerCase()=="th")
// continue;
if(el.nodeName.toLowerCase()=="tr")
break;

Quick and dirty does the trick:
if( el.cells[0].nodeName.toLowerCase()=="th" ) { return; }
document.ds.server.value = el.cells[0].textContent;
document.ds.drive.value = el.cells[1].textContent;
document.ds.submit();
}

I'm calling it in the onclick event for a Table with id="ds". It works
by allowing me to click any row in the table and passing the first 2 cells
as hidden input fields, but I can't figure out how to skip the Tableheads.
I'm using another script to be able to sort the rows by clicking the table
head row and I don't want this to run when I do that.

Another idea to modify the sorting function to stop the event from
propagating:

if( e.stopPropagation ) {
e.stopPropagation();
}else if() {
e.cancelBubble = true;
}

hth
ivo
http://www.yorick.onlyfools.com/
 
R

RobG

Matt said:
I have the following function that I found and modified from another post

function setValues(evt, table){
var el;
evt=evt||window.event;
el=evt.target||evt.srcElement;

for(;el&&el!=table;el=el.parentNode)
//if(el.nodeName.toLowerCase()!="th")
// if(el.nodeName.toLowerCase()=="th")
// continue;
if(el.nodeName.toLowerCase()=="tr")
break;

document.ds.server.value = el.cells[0].textContent;
document.ds.drive.value = el.cells[1].textContent;
document.ds.submit();
}

I'm calling it in the onclick event for a Table with id="ds". It works great
by allowing me to click any row in the table and passing the first 2 cells
as hidden input fields, but I can't figure out how to skip the Table heads.
I'm using another script to be able to sort the rows by clicking the table
head row and I don't want this to run when I do that. The commented lines
are the things that I have tried so far and failed.

Any help appreciated.

Some suggestions:

1. Attach the onclick to rows but not those with TH cells in them
(rowIndex 0?). You could use an onload function to do it.

2. As go up the node tree to the TR parent, bail out if you encounter
a th element (see below).

3. If the TR parent's rowIndex is 0 (presumably the TH cells are the
first row) then don't do stuff.

Here is a routine that will bail out if a th is encountered:

function setValues(evt, table){
evt = evt || window.event;
var el = evt.target || evt.srcElement;

while ( el.parentNode && 'tr' != el.nodeName.toLowerCase() ) {
if ('th' == el.nodeName.toLowerCase() ) return;
el = el.parentNode;
}
// Do stuff
}

Untested but it should work.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top