[Javascript] struts form using javascript code
Chris Tifer
christ at saeweb.com
Wed Mar 3 10:52:51 CST 2004
Here's an example to show you what I'm talking about:
============================================
<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="text" name="Test" value="Hello">
<input type="Submit" value="Test It">
</form>
============================================
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>
============================================
----- Original Message -----
From: "Hassan Schroeder" <hassan at webtuitive.com>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Wednesday, March 03, 2004 11:28 AM
Subject: Re: [Javascript] struts form using javascript code
> Chris Tifer wrote:
>
> > I'm pretty sure I've already solved it. There's only one "list" element
> > and he's trying to check the length on it.
>
> That's OK, a length of 1 is legal :-)
>
> > He can either add more of them and check the length ( a collection ) or
> > access that field's value and check the length on that.
>
> Actually, first he's declaring the Array incorrectly:
>
> menus = new Array; /* right: menus = new Array(); */
>
> var checkBoxes = driversDisplayForm.elements["list"];
>
> /* :: and then, here in this loop, "menus" is being treated as
> a string, not an array; declared as a string, this works...
> */
>
> for(var i = 0; i < checkBoxes.length; i++)
> {
> if(checkBoxes[i].checked)
> {
> menus = menus+","+i; // string concatenation in progress :-)
> value = true;
> }
> }
>
> /* Just generically speaking, using 'value'*, an attribute of many
> objects, as a variable name seems like an insanely bad idea to
> me -- even if it's not on the list of reserved words...
>
> *earlier declared as: var value = false;
> */
>
> YMMV!
> --
> Hassan Schroeder ----------------------------- hassan at webtuitive.com
> Webtuitive Design === (+1) 408-938-0567 === http://webtuitive.com
>
> dream. code.
>
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
More information about the Javascript
mailing list