check some radiobuttons

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bjoern Jackschina

    #1

    check some radiobuttons

    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="JavaS cript"><!--
    function pruefen(){retur n true}
    //-->
    </script>
    <script language="JAvaS cript1.1"><!--
    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)*/

    for(i=0; i < f.elem.length; i++){

    if ('checkbox' == elem[i].type)
    {
    if(elem[i].checked)
    k++;
    }
    }return true; //(or k)
    }
    //-->
    After that I need to check k.

    <form onSubmit="retur n pruefen(this)"> </form>

    Many thanks
  • Michael Winter

    #2
    Re: check some radiobuttons

    Bjoern Jackschina wrote on 02 Dec 2003:
    [color=blue]
    > 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="JavaS cript"><!--
    > function pruefen(){retur n true}
    > //-->
    > </script>
    > <script language="JAvaS cript1.1"><!--[/color]

    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
    [color=blue]
    > 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)*/[/color]

    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
    [color=blue]
    > for(i=0; i < f.elem.length; i++){
    >
    > if ('checkbox' == elem[i].type)
    > {
    > if(elem[i].checked)
    > k++;
    > }
    > }return true; //(or k)
    > }[/color]

    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.leng th; ++i ) {
    if ('checkbox' == elem[i].type) {
    if (elem[i].checked) k++;
    }
    }
    return true;
    [color=blue]
    > //-->
    > After that I need to check k.[/color]

    How, where, and what for?
    [color=blue]
    > <form onSubmit="retur n pruefen(this)"> </form>[/color]

    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.Winter@blueyo nder.co.uk.invalid (remove ".invalid" to reply)

    Comment

    Working...