[thelist] JS, counting selected checkboxes

Warden, Matt mwarden at odyssey-design.com
Sun Jan 21 15:03:31 CST 2001


untested...




var counter=0;

// edit the below two lines
var checkboxfieldname = document.forms[0].checkboxfieldname;
var maxselects = 9

myloop: for (var i=0; i<checkboxfieldname.length; i++)
{
    if (checkboxfieldname[i].selected)
    {
        counter++;
        if (counter >= maxselects)
        {
            alert("You can only select nine checkboxes, do0d!");
            exit myloop;
        }
    }
}

Dunno if the myloop:/exit myloop is valid JS syntax. If not, just get rid of
the myloop: and change the latter to just exit;.

Should be pretty fast, since the counter and maxselects vars are only compared
if the counter is incremented.


hth,

--
mattwarden
mattwarden.com



----- Original Message -----
From: aardvark <roselli at earthlink.net>
To: <thelist at lists.evolt.org>
Sent: Sunday, January 21, 2001 10:31 AM
Subject: [thelist] JS, counting selected checkboxes


>
> i'm trying to find some JS that, on form submit or on selection of
> the 10th item, will notify the user that he/she has gone beyond the
> limit...
>
> i have a screen with a boatload of checkboxes, depending on some
> user prefs, but the user can only select nine items... right now the
> server-side script handles it, but i've found that the users get mired
> down when the page redraws (even with all the checkboxes
> selected and a warning about the nine items), so for those who can
> support it, i'd like a little JS alert to warn them that they are going
> over the nine item limit...
>
> make sense?
>
> tried a few sites, can't find anything... i generally suck with this
> kind of stuff anyway...
>
> ---------------------------------------
> For unsubscribe and other options, including
> the Tip Harvester and archive of TheList go to:
> http://lists.evolt.org Workers of the Web, evolt !
>





More information about the thelist mailing list