...

View Full Version : reference second table of form??



nikko50
05-30-2009, 03:37 PM
I'm looping thru all the document form elements with the below loop. While looping I would like to know if this element is from the second table in the form. My issue is the table has no name. How can I know if I'm dealing with an element that is in the second table of the form???





var field = document.forms['ovrFrm'].elements;

for (i = 0; i < field.length; i++) {
//DO SOMETHING HERE

}

adios
05-30-2009, 06:08 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>untitled</title>
<script type="text/javascript">

function fn()
{
var f = document.getElementById('ovrFrm');
var i, el, field, fields = f.elements;
for (i = 0; i < fields.length; i++)
{
el = field = fields[i];
do
el = el.parentNode;
while (el && el.nodeName != 'TABLE');
if (el == f.getElementsByTagName('table')[0])
field.value = '... in 1st table.';
else if (el == f.getElementsByTagName('table')[1])
field.value = '... in 2nd table.';
else alert('not found');
}
}

function addListener(obj, evt, handler)
{
if (obj.addEventListener)
{
obj.addEventListener(evt, handler, false);
}
else if (obj.attachEvent)
{
obj.attachEvent('on' + evt, handler);
}
}

addListener(window, 'load', fn);

</script>
</head>
<body>
<form id="ovrFrm">
<table border="1" bgcolor="pink">
<thead>
<tbody>
<tr><td>1st table
<input type="text" name="1st field" value="1st field" />
</td></tr>
</tbody>
</table>
<hr />
<table border="1" bgcolor="tan">
<thead>
<tbody>
<tr><td>2nd table
<input type="text" name="2nd field" value="2nd field" />
</td></tr>
</tbody>
</table>
</form>
</body>
</html>

https://developer.mozilla.org/en/traversing_an_html_table_with_javascript_and_dom_interfaces

mrhoo
05-30-2009, 09:00 PM
{
var F= document.forms['ovrFrm'], field= F.elements;
var el, pa, T= F.getElementsByTagName('table');
for(var i= 0, L= field.length; i<L; i++){
el= field[i];
pa= el.parentNode;
while(pa && pa.parentNode){
if(pa== T[0]) pa= 1;
else if(pa== T[1]) pa= 2;
else(if pa== F) pa= null; // not in either table
}
//pa is 1, 2, or null, depending on the containing table
}
}

If you often need to lookup the document tree, to see if an element
is an ancestor of another, you can make it a separate method.


document.isPa= function(who, pa){
while(who.parentNode){
if(who== pa) return true;
who= who.parentNode;
}
return false;
}


var F= document.forms['ovrFrm'], field= F.elements;
var el, pa, T= F.getElementsByTagName('table');
for(var i= 0, L= field.length; i<L; i++){
el= field[i];
if(document.isPa(el, T[0])) pa= 1;
else pa= document.isPa(el, T[1])? 2: null;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum