Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Re: Simple question I hope


Message #1 by "Susan Henesy" <susan.henesy@u...> on Wed, 5 Mar 2003 17:57:52
Debbie,

Fell across this thread and just wanted to mention something.  You asked 
if there was a way to detect a field element's type -- and you did find a 
way to do that, using the getAttribute.

There's another way to do it, too -- this works if your elements are 
within <FORM> tags and might be a little more direct:

function seeFieldTypes() {
   var i = 0;
   var frm = document.forms[0]; // Represents the first form on the page.
                                // If more than one form, then the next
                                // one would be document.forms[1].  
                                // Just a more global way of referencing
                                // forms, rather than by id or name.
   
   // Loop through each form element
   // and alert the user to its NAME and
   // TYPE.
   for (i = 0; i < frm.length; i++) {
       var el = frm.elements[i];

       alert("Element Name and Type: " + el.name + ", " + el.type}
   }

}

So, you can go right to the element's TYPE property.  I've done this many 
times before, programming code like: 
//-------------------------------------------------------------------
   for (i = 0; i < frm.length; i++) {
       var el = frm.elements[i];
       //alert("Element Name and Type: " + el.name + ", " + el.type);
       switch (el.type) {
		case "select-one":
			alert("I am a combo box " + el.name);
                        break;
         	case "text":
			alert("I am text " + el.name);
                        break;
                case "hidden":
			alert("I am hidden " + el.name);
                        break;
	        default:
                        alert("I am " + el.type + " " + el.name);
			break;
       
       }       
   }

//-------------------------------------------------------------------

There are some elements that are not detected in <FORM> tags.  The one I 
am acutely aware of are LABELS, because they are children of the DOCUMENT, 
not the FORM -- even when they're contained in FORM tags!  Drives me nuts, 
but I can't fight the system.  I wind up using document.getElementByTag 
for <LABEL>s.

Hope you find this informative for future coding adventures!

Susan 


  Return to Index