if(document.forms[f].element[e].type == "checkbox")

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

    if(document.forms[f].element[e].type == "checkbox")

    I want to know what's wrong with this code (I'm an amateur programmer).
    I'm trying to check if every field has a value or if checkboxes/radios
    have at least one item checked on each group (yes, you know, with the
    same name in the HTML tag).
    I want to have a generic code which I can fit to every document.

    The problem is that it never got into the line:
    if(elm.type == "checkbox" || elm.type == "radio")
    I've put an alert(elm.type)




    function checkallfields( ) {

    var anychecked = true;

    for(f = 0; f < document.forms. length ; f++) {
    frm = document.forms[f];
    for(e = 0; e < frm.elements.le ngth; e++) {
    elm = frm.elements[e];
    if(elm.type == "checkbox" || elm.type == "radio") {
    var passcheck = false;
    for(c = 0; c < frm[elm.name].lenght; c++) {
    n = e + c;
    passcheck = passcheck || frm.element[n].checked;
    }
    e += c
    anychecked = anychecked && passcheck;
    } else {
    if(elm.value == "") anychecked = anychecked && false;
    }
    }
    }
    return anychecked;
    }





    Well, here's the form which I'm doing the tests with:

    <form method="POST" action="process .php"
    onsubmit="retur n checkallfields( )">

    <input type="hidden" name="cat" value="1">
    e-mail:<input type="text" name="email_res puesta" size="20">

    <br>textarea:<t extarea rows="2" name="textarea1 " cols="20"></textarea>
    <br>checkboxes: <input type="checkbox" name="checkboxe s1" value="A">
    <input type="checkbox" name="checkboxe s1" value="B">
    <br>radios:<inp ut type="radio" value="V1" checked name="radios1">
    <input type="radio" name="radios1" value="V2">
    <br><input type="submit" value="Send">
    <input type="reset" value="Clear">
    <input type="button" value="check" onclick="alert( checkallfields( ));">
    </form>



    Thank you very much!
  • kaeli

    #2
    Re: if(document.for ms[f].element[e].type == &quot;checkbox& quot;)

    In article <B_jeb.50448$FN 3.3503392@news. ono.com>, phisys@netscape .net
    enlightened us with...[color=blue]
    > I want to know what's wrong with this code (I'm an amateur programmer).[/color]

    I think it has a couple typos and doesn't check radio buttons right,
    assuming you copied/pasted.
    [color=blue]
    > if(elm.type == "checkbox" || elm.type == "radio") {
    > var passcheck = false;
    > for(c = 0; c < frm[elm.name].lenght; c++)[/color]

    length, not lenght.

    {[color=blue]
    > n = e + c;
    > passcheck = passcheck || frm.element[n].checked;[/color]

    frm.elements[n], not frm.element[n]

    Can't check if it's checked this way for radio buttons.
    Radio buttons are an array because they have the same name. Checkboxes
    are not (unless more than one with same name).
    Need array subscript for radio buttons. Loop through with another for
    loop. Would be like
    frm.elements[n][i].checked
    for radios.

    See archives and another post I made a few days ago on how to loop
    through radio buttons.

    -------------------------------------------------
    ~kaeli~
    All I ask for is the chance to prove that money
    cannot make me happy.


    -------------------------------------------------

    Comment

    • Lee

      #3
      Re: if(document.for ms[f].element[e].type == &quot;checkbox& quot;)

      PhiSYS said:[color=blue]
      >
      >I want to know what's wrong with this code (I'm an amateur programmer).
      >I'm trying to check if every field has a value or if checkboxes/radios
      >have at least one item checked on each group (yes, you know, with the
      >same name in the HTML tag).
      >I want to have a generic code which I can fit to every document.
      >
      >The problem is that it never got into the line:
      >if(elm.type == "checkbox" || elm.type == "radio")
      >I've put an alert(elm.type)[/color]

      When I put an alert right after that "if", I see the alert.
      What might be causing trouble is the typo of "length":
      [color=blue]
      > for(c = 0; c < frm[elm.name].lenght; c++) {[/color]

      Comment

      • DU

        #4
        Re: if(document.for ms[f].element[e].type == &quot;checkbox& quot;)

        kaeli wrote:
        [color=blue]
        > In article <B_jeb.50448$FN 3.3503392@news. ono.com>, phisys@netscape .net
        > enlightened us with...
        >[color=green]
        >>I want to know what's wrong with this code (I'm an amateur programmer).[/color]
        >
        >
        > I think it has a couple typos and doesn't check radio buttons right,
        > assuming you copied/pasted.
        >
        >[color=green]
        >> if(elm.type == "checkbox" || elm.type == "radio") {
        >> var passcheck = false;
        >> for(c = 0; c < frm[elm.name].lenght; c++)[/color]
        >
        >
        > length, not lenght.
        >[/color]

        The code given still does not make sense. There is no length attribute
        for checkbox. When elm is a checkbox, then frm[elm.name].length can not
        be executed.
        [color=blue]
        > {
        >[color=green]
        >> n = e + c;[/color][/color]


        n = e + c;
        is a bad coding practice that is always bound to create confusion, more
        time spent into debugging code. No meaningful, intuitive variable
        identifiers.

        DU
        --
        Javascript and Browser bugs:

        - Resources, help and tips for Netscape 7.x users and Composer
        - Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x


        Comment

        • kaeli

          #5
          Re: if(document.for ms[f].element[e].type == &quot;checkbox& quot;)

          In article <blcn0h$e7c$1@n ews.eusc.inter. net>, drunclear@hot-R-E-M-O-V-
          E-mail.com enlightened us with...[color=blue]
          >
          > The code given still does not make sense. There is no length attribute
          > for checkbox. When elm is a checkbox, then frm[elm.name].length can not
          > be executed.
          >[/color]

          Right. Normally.
          Unless, however, he has several checkboxes of the same name.
          Which he does.

          See his original post, including html source.

          -------------------------------------------------
          ~kaeli~
          All I ask for is the chance to prove that money
          cannot make me happy.


          -------------------------------------------------

          Comment

          • PhiSYS

            #6
            if(document.for ms[f].element[e].type == &quot;checkbox& quot;)

            God! My code was catastrophic.
            Thank you all.

            I make it work with the following code:





            function checkallfields( ) {

            var anychecked = true;

            for(f = 0; f < document.forms. length ; f++) {
            frm = document.forms[f];
            for(e = 0; e < frm.elements.le ngth; e++) {
            elm = document.forms[f].elements[e];
            if((elm.type == "checkbox") || (elm.type == "radio")) {
            var passcheck = false;
            var elm_name = elm.name;
            var elm_length = frm[elm_name].length;
            for(c = 0; c < elm_length; c++) {
            n = e + c;
            passcheck = passcheck || frm.elements[n].checked;
            }
            e = e + (elm_length - 1)
            anychecked = anychecked && passcheck;
            } else {
            if(elm.value == "") anychecked = anychecked && false;
            }
            }
            }
            return anychecked;
            }

            Comment

            Working...