/    Sign up×
Community /Pin to ProfileBookmark

Check Box Counter

Hi There,

i need the check box counter to check 3 different catagories of checkboxes so that i get 3 seperate counts in the same form. (is this possible)

either with the same push button or three seperate ones.

i have tried to adjust the code by changing the [B]onClick [/B]values, the [B]checkbox [/B]name individually & at the same time the but have had no luck.

can any body shed some light?

thanks in advance

avgo

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@gtzpowerApr 01.2007 — umm, what?

I don't have any idea what you're talking about when you say counts or counters. Can you explain what you're trying to do a bit better or give an example?
Copy linkTweet thisAlerts:
@avgosauthorApr 01.2007 — thanks for your prompt reply.

[U]THE STORY SO FAR...[/U]

i have used the 'check box counter' located at:

[U]http://javascript.internet.com/forms/checkbox-counter.html[/U]

to count the number of checkboxes that have been checked in a form. this works great. (i have also included the 'cookie form saver' in the same html page (found at)):

http://javascript.internet.com/forms/cookie-form-saver.html

they both work together brilliantly. take a look at the page here: http://www.evagoras.co.uk/checkboxcount2.htm

[U]MY SCENARIO:[/U]

I am a wedding photographer and the page in question is to be used by my clients to choose images for their wedding album plus any reprints they need (6x4' & 7x5'). i need the checkbox counter to count 3 individual variables:

6x4, 7x5 & album.

at the moment if (for example) they check the 6x4, 7x5 & the album checkboxes under image 1 the check box counter will return:

"You have Selected 3 Image(s)"

i need the 'checkbox counter' to count the 3 variables individually so that it returns (with the above example):

You have selected 1 6x4 Image(s)

You have selected 1 7x5 Image(s)

You have selected 1 album Image(s)

i dont mind if the form has an individual push button for each variable

thanks again for your prompt reply and your help.
Copy linkTweet thisAlerts:
@gtzpowerApr 01.2007 — [CODE]
function anyCheck(form) {
var total = 0;
var max = form.ckbox.length;
for (var idx = 0; idx < max; idx++) {
if (eval("document.yourform.ckbox[" + idx + "].checked") == true) {
total += 1;
}
}
[/CODE]


In your anyCheck() function, add 2 more loops, but have each loop tally up a different name. Then you can name all of your 6x4 boxes to ckbox6x4, 7x5 to ckbox7x5, and Albums to ckboxAlbum. See the short script below that should work after they are renamed, but it is untested (and there is probably a better way, but this should get you going for now). Good luck!

[CODE]
function anyCheck(form) {
var total6x4 = 0;
var max6x4 = form.ckbox6x4.length;
for (var idx = 0; idx < max6x4; idx++) {
if (eval("document.yourform.ckbox6x4[" + idx + "].checked") == true) {
total6x4 += 1;
}
}

var total7x5 = 0;
var max7x5 = form.ckbox7x5.length;
for (var idx = 0; idx < max7x5; idx++) {
if (eval("document.yourform.ckbox7x5[" + idx + "].checked") == true) {
total7x5 += 1;
}
}

var totalAlbum = 0;
var maxAlbum = form.ckboxAlbum.length;
for (var idx = 0; idx < maxAlbum; idx++) {
if (eval("document.yourform.ckboxAlbum[" + idx + "].checked") == true) {
totalAlbum += 1;
}
}

var grandTotal = total6x4 + total7x5+ totalAlbum;
}
[/CODE]
Copy linkTweet thisAlerts:
@avgosauthorApr 01.2007 — thanks for your help.

i've put the code in my html but there is an error when the 'image count' push button is pushed.

error: "total is undefined"

im guessing its at the end of the script (well thats line number was given in the error)

im not too sure where the:

var grandTotal = total6x4 + total7x5+ totalAlbum;

alert("You've selected " + total + " image(s).");

at the end or where they go in the brackets


function anyCheck(form)

{

var total6x4 = 0;

var max6x4 = form.ckbox6x4.length;

for (var idx = 0; idx < max6x4; idx++)

{

if (eval("document.yourform.ckbox6x4[" + idx + "].checked") == true)

{

total6x4 += 1;

}

}

var total7x5 = 0;

var max7x5 = form.ckbox7x5.length;

for (var idx = 0; idx < max7x5; idx++)

{

if (eval("document.yourform.ckbox7x5[" + idx + "].checked") == true)

{

total7x5 += 1;

}

}

var totalAlbum = 0;

var maxAlbum = form.ckboxAlbum.length;

for (var idx = 0; idx < maxAlbum; idx++)

{

if (eval("document.yourform.ckboxAlbum[" + idx + "].checked") == true)

{

totalAlbum += 1;

}

}

var grandTotal = total6x4 + total7x5+ totalAlbum;

alert("You've selected " + total + " image(s).");

}

// End -->
Copy linkTweet thisAlerts:
@gtzpowerApr 02.2007 — that's because nowhere in the script is there a total = something. I was just showing you a way to get the values you need into a variable. You will need to modify it to do what you need. use:

[CODE]
alert("You've selected " + grandTotal + " total image(s).");
alert("You've selected " + total6x4 + " 6x4 image(s).");
alert("You've selected " + total7x5 + " 7x5 image(s).");
alert("You've selected " + totalAlbum + " album(s).");
[/CODE]


Don't forget to update your HTML checkbox names too or this will not work.
Copy linkTweet thisAlerts:
@avgosauthorApr 02.2007 — WOW!

You're great! It works perfectly!!!!

Thanks for all your help.

evagoras (avgos)
Copy linkTweet thisAlerts:
@avgosauthorApr 07.2007 — thanks for all your help so far, you've been great.

althought the page works perfectly it's tooooo huge. its over 4MB and thats only half of what i intend it to be.

take a look at:

http://www.evagoras.co.uk/gallery/xx-xx-xx/imageselections/

[B][COLOR="Red"]warning![/COLOR] it may slow down your computer.[/B]

firstly, it takes ages to load.

secondly, the image count button (checkbox count) takes ages to execute as it cycles through the JS. and i mean ages!

is there any way that i can split the current page between a number of pages and keep the check box selection in some sort of database/cookie - and still be able to have the same check box count execution at the end of my client's session.

i spent all that time designing and i've come to a dead end.

thanks again for all your help.

regards

avgos
×

Success!

Help @avgos 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...