Bjoern Jackschina wrote on 02 Dec 2003:
Hello,
until now I have not found the mistake that I made. My source
code for checking how many checkboxes or radiobuttons are used:
<script language="JavaScript"><!--
function pruefen(){return true}
//-->
</script>
<script language="JAvaScript1.1"><!--
1) Don't use SGML comments in SCRIPT blocks. It's not necessary.
2) Don't use the language attribute. It has been deprecated in favour
of type (type="text/javascript"). Moreover, type is /required/,
language is not.
3) Version 1.3 of JavaScript is four years old. Finding a browser
that does not support it would be difficult enough to find. The odds
of coming across a browser that doesn't support version 1.1 are even
smaller.
4) As language is deprecated, browsers don't have to use it to
determine what scripts to execute and what scripts to ignore.
Besides, there's a better way of testing if a property or method is
implemented:
if ( 'undefined' != typeof object.property ) {
// property supported
OR
if ( undefined != object.property ) {
// property supported
function pruefen(f){
var i;
var k=0;
document.forms[f].elements;/* here I can't find any syntax for
an object(mainly I was trying with a book and there is no
description about objects)*/
When you called this function in the intrinsic event, you passed the
entire form object reference (this). That means that 'f' is
equivalent to:
document.forms['nameOfFormF']
If you want the elements collection, all you need do is:
f.elements
for(i=0; i < f.elem.length; i++){
if ('checkbox' == elem[i].type)
{
if(elem[i].checked)
k++;
}
}return true; //(or k)
}
This block is very difficult to read. That is what formatting is for.
You should have presented it like this before posting it:
for ( i = 0; i < f.elements.length; ++i ) {
if ('checkbox' == elem[i].type) {
if (elem[i].checked) k++;
}
}
return true;
//-->
After that I need to check k.
How, where, and what for?
<form onSubmit="return pruefen(this)"></form>
Replace both SCRIPT blocks with this:
<SCRIPT type="text/javascript">
function pruefen( f ) {
var k = 0, elems = f.elements;
var size = elems.length;
if (size) {
if ('undefined' == typeof elems[0].type) {
// The type property is the only non-1.0 property used in
// this function. The document.forms collection is v1.1, but
// we get the array via the this operator. The length
// property /should/ be v1.0 as other length properties are.
// If this block is entered, type is not defined.
}
}
for (var i = 0; i < size; ++i ) {
if ('checkbox' = elems[ i ].type) {
if (elems[ i ].checked) k++;
}
}
}
</SCRIPT>
I don't know what you wanted to do with k (how and when you were
going to check it), so I just incremented it. If you want to access
it outside of this function, you'll either have to return it, or
declare it global (declare elems with var, though). You also didn't
say under what conditions this function might return true or false,
so I didn't include that either. If the submission should only occur
if one or more boxes were checked, you could return k (if zero, it
will evaluate as false, or as true if one or more).
Hope that helps somewhat,
Mike
--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)