/    Sign up×
Community /Pin to ProfileBookmark

nested for loops

hoping someone can help……based on reading here and elsewhere, i thought this should work, but it doesn’t:

function threechecks()
{
test=0
for (var c=1;c<=3;c++)
{
var datecount=document.lunch[“choice”+c].length
for (var r=0;r<=datecount;r++)
{
if (document.lunch[“choice”+c][r].checked==true)
{
alert(“We found a check item for choice #”+c)
test=test+1
}
}
alert(test)
}
alert(“test value = “+test)
if (test<3) {return false}
}

and the function is called by onsubmit=”return threechecks()”

my form (named “lunch”) has a varying number of choices, from which the visitor must select one “choice1”, one “choice2” and one “choice3”. the above script loops just fine through the outer for loop, and will identify a checked item for “choice1”, but then it just skips the rest of the code, and submits the form. if there’s no selection for “choice1” it submits the form. in fact, it quits the function without showing the “alert(test)” that i put in after the inner for loop.

can anyone see where i’m going wrong?

tia,
janaki

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@MikeFosterSep 02.2004 — Hi janaki,

Here's a few suggestions, but I really need to see the form's html before going any further.

  • 1. Always terminate statements with a semicolon ';'.


  • 2. The variable 'datecount' gets created/destroyed 3 times - need to move the declaration out of the loop. The same goes for variable 'r'.


  • 3. The expression [i]document.lunch[/i] may work in IE, but [i]f = document.forms['lunch'][/i] is much more cross-browser. Now if there is a form control named 'choice1'... [i]f.elements['choice1'][/i]. If the form controls have [b]id[/b]s instead of [b]name[/b]s then you can use [i]document.getElementById('choice1')[/i].
  • Copy linkTweet thisAlerts:
    @janakiauthorSep 02.2004 — Mike,

    thanks for the helpful suggestions.

    OK, i think i've put in all the changes you suggested, and i've inserted some alerts to keep track of what's happening (shown at the bottom). it loops fine through the first "for" where c=1, and it checks each value of r, but then it just quits and submits the form. unless i'm not seeing something, i would think that the way this is currently written, it should [I]never[/I] submit since i've put "return false;" at the outside of both loops, but it ignores it. why??

    the HTML of the relevant form is generated from a database, but ends up starting off like this:

    <form name="lunch" method="post" action="/WALS/WALSlunch.asp" onsubmit="return threechecks()">

    <input type="hidden" name="FAREID" value="93">

    <TABLE cellpadding="2"><tr><th colspan="3">Choice</th></tr>

    <tr><th>1</th><th>2</th><th>3</th><th>Date</th>

    <th>Speaker</th>

    <th>LectureTitle</th>

    </tr>

    <tr><td><input type="radio" name="choice1" value="9/8/2004" onClick="checkradios(1,0)"></td>

    <td><input type="radio" name="choice2" value="9/8/2004" onClick="checkradios(2,0)"></td>

    <td><input type="radio" name="choice3" value="9/8/2004" onClick="checkradios(3,0)"></td></td><td>9/8/2004</td>

    .

    .

    .

    and goes through a variable number of rows of buttons....

    function threechecks()

    {

    var test=0;

    var rows=document.forms['lunch']['choice1'].length;

    var c;

    var r;

    for (c=1;c<=3;c++){

    alert("we're checking choice #"+c);

    for (r=0;r<=rows;r++) {

    if (document.forms['lunch']['choice'+c][r].checked==true) {

    test=test+1;

    alert("We found a check item for choice #"+c+" and test="+test);

    }

    else {alert("button #"+r+" for choice "+c+" is not checked");}

    }

    }

    alert("test value = "+test);

    return false;

    }
    Copy linkTweet thisAlerts:
    @MikeFosterSep 02.2004 — I played around with it a little. Not perfect, but maybe it will be helpful.
    <i>
    </i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
    &lt;script type='text/javascript'&gt;

    var log = ''; //// debug ////

    function verifyLunch()
    {
    var f = document.forms['lunch'];
    var cn; // choice number
    var nl, nli; // node list, and index, of radio input objects
    var re; // radio input element object
    var res = new Array(); // boolean results per radio group

    log = '* verifyLunch() test log *nn'; //// debug ////
    nl = 1; // prime the loop ;-)
    for (cn = 1; nl; ++cn) {
    nl = f.elements['choice' + cn];
    if (nl) {
    res[cn-1] = false; // assume guilty until proven otherwise ;-)
    for (nli = 0; nli &lt; nl.length; ++nli) {
    re = nl[nli];
    if (re.checked) {
    res[cn-1] = true;
    }
    log += 'name: ' + re.name +
    ', value: ' + re.value +
    ', checked: ' + re.checked + 'n'; //// debug ////
    }
    log += 'n'; //// debug ////
    }
    }
    alert(log + 'nVerification Results By Radio Group:nn' + res); //// debug ////
    for (var i = 0; i &lt; res.length; ++i) {
    if (!res[i]) {
    return false;
    }
    }
    return true;
    }

    &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;

    &lt;form name='lunch' method='post' action='action.php' onsubmit='return verifyLunch()'&gt;
    &lt;h3&gt;Choice 1&lt;/h3&gt;
    &lt;p&gt;&lt;input type='radio' name='choice1' value='1'&gt;sub-choice 1&lt;/p&gt;
    &lt;p&gt;&lt;input type='radio' name='choice1' value='2'&gt;sub-choice 2&lt;/p&gt;
    &lt;p&gt;&lt;input type='radio' name='choice1' value='3'&gt;sub-choice 3&lt;/p&gt;
    &lt;h3&gt;Choice 2&lt;/h3&gt;
    &lt;p&gt;&lt;input type='radio' name='choice2' value='1'&gt;sub-choice 1&lt;/p&gt;
    &lt;p&gt;&lt;input type='radio' name='choice2' value='2'&gt;sub-choice 2&lt;/p&gt;
    &lt;p&gt;&lt;input type='radio' name='choice2' value='3'&gt;sub-choice 3&lt;/p&gt;
    &lt;h3&gt;Choice 3&lt;/h3&gt;
    &lt;p&gt;&lt;input type='radio' name='choice3' value='1'&gt;sub-choice 1&lt;/p&gt;
    &lt;p&gt;&lt;input type='radio' name='choice3' value='2'&gt;sub-choice 2&lt;/p&gt;
    &lt;p&gt;&lt;input type='radio' name='choice3' value='3'&gt;sub-choice 3&lt;/p&gt;
    &lt;p&gt;&lt;input type='submit'&gt;&lt;/p&gt;
    &lt;/form&gt;

    &lt;/body&gt;
    &lt;/html&gt;
    Copy linkTweet thisAlerts:
    @janakiauthorSep 02.2004 — that's brilliant!! works great!! and more generalizable than what i had.

    i really appreciate the time you put into this........i definitely didn't expect you to write the script for me from scratch!!

    i'll have to study this a bit more...i'm still a bit baffled by why the code that i had wouldn't loop properly, but i'm sure if i gaze at it enough, it'll become clear, right? ?
    ×

    Success!

    Help @janaki spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.13,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...