Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    reference second table of form??

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




    Code:
    var field = document.forms['ovrFrm'].elements;
    
      for (i = 0; i < field.length; i++) {
    //DO SOMETHING HERE
    
    }

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Code:
    <!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/tra...dom_interfaces

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Code:
    {
    	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.

    Code:
    document.isPa= function(who, pa){
    	while(who.parentNode){
    		if(who== pa) return true;		
    		who= who.parentNode;
    	}
    	return false;
    }
    Code:
    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;
    }
    Last edited by mrhoo; 05-30-2009 at 10:02 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •