[Javascript] struts form using javascript code

Hassan Schroeder hassan at webtuitive.com
Wed Mar 3 11:19:57 CST 2004


Chris Tifer wrote:

> Here's an example to show you what I'm talking about:

> That will work because there's 2 "Test" fields. The following will not work
> because there's only one. There is no collection, hence, there is no .length
> property of it.

> <script language="javascript">
> function testIt(){
>  var objForm = document.forms["myForm"]
>  alert(objForm.elements["Test"].length)
>  return false
> }
> </script>

> <form name="myForm" onSubmit="return testIt()">
> <input type="text" name="Test" value="Hello">
> 
> <input type="Submit" value="Test It">
> </form>

You're right. I've never run into that before, but it feels like
an implementation bug to me. An Array length of 1 *is* legal.

If you /explicitly/ create an array of all "Test" inputs, as below:

function testIt(){
	var TESTS = new Array();
	allInputs = document.getElementsByTagName("input");
	for ( i = 0; i < allInputs.length; i++ )
	{
		if (allInputs[i].name == "Test")
		{
			TESTS[i] = "Test";
		}
	}	
	alert(TESTS.length);

	return false;
}

:: you'll get back a length of 1 (for your second example).

It's good to be aware of that behavior, though; thanks for pointing
it out!
-- 
Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com

                           dream.  code.





More information about the Javascript mailing list